This repository was archived by the owner on Jul 11, 2025. It is now read-only.
forked from google/j2objc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_java_source_jar.py
More file actions
executable file
·107 lines (94 loc) · 3.26 KB
/
Copy pathgen_java_source_jar.py
File metadata and controls
executable file
·107 lines (94 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Script for generating a jar file from a javac-like sourcepath and a
list of Java source files.
Usage:
$ gen_java_source_jar.py [-h] [-sourcepath <path>] -o output-file ...
"""
# Used by argparse to build help string.
USAGE_STRING ="""
Generates a jar file that contains Java sources in a layout useful
for debugger use. Source file names may be relative, starting with
their package directory, such as "java/lang/String.java". The
-sourcepath argument specifies the root directories to search for
the specified source files."""
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
def BuildSourceJar(jar_file, sources):
"""Creates a jar file of a list of source files."""
staging_dir = tempfile.mkdtemp()
for f in sources.keys():
temp_src = os.path.join(staging_dir, f)
pkg_dir = os.path.dirname(temp_src)
if not os.path.exists(pkg_dir):
os.makedirs(os.path.dirname(temp_src))
shutil.copyfile(sources[f], temp_src)
out_file = os.path.join(os.getcwd(), jar_file)
proc = subprocess.Popen("jar cf {} *".format(out_file), shell=True,
cwd=staging_dir)
proc.wait()
shutil.rmtree(staging_dir)
return proc.returncode;
def GetSourcePath(file, sourcepath):
"""Locates a relative file in a list of source paths."""
for root in sourcepath:
path = os.path.join(root, file)
if os.path.exists(path):
return path
return None
def GetSourceFile(file, sourcepath):
"""Return a relative file if it is embedded in a path."""
for root in sourcepath:
if file.find(root) == 0:
prefix_length = len(root)
if not root.endswith('/'):
prefix_length += 1
relative_file = file[prefix_length:]
return relative_file
return None
if __name__ == "__main__":
errors = 0
parser = argparse.ArgumentParser(description=USAGE_STRING)
parser.add_argument("-sourcepath", metavar="<path>",
help="specify where to find source files")
parser.add_argument("-o", metavar="jar-file", help="the jar file to create")
parser.add_argument("java_files", help="Java source files",
nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.o == None:
print("error: no jar file specified")
errors += 1
if args.sourcepath == None:
sourcepath = [ '.' ]
else:
sourcepath = args.sourcepath.split(':')
file_map = {}
for f in args.java_files:
relative_file = GetSourceFile(f, sourcepath)
if relative_file:
f = relative_file
path = GetSourcePath(f, sourcepath)
if path:
file_map[f] = path
else:
print("file not found: {}".format(file))
errors += 1
if not errors:
errors = BuildSourceJar(args.o, file_map)
sys.exit(errors)