22
22
import com .google .copybara .util .FileUtil ;
23
23
import com .google .copybara .util .FileUtil .ResolvedSymlink ;
24
24
import com .google .copybara .util .Glob ;
25
- import com .google .devtools .build .lib .events .Location ;
26
25
import com .google .devtools .build .lib .skylarkinterface .Param ;
27
26
import com .google .devtools .build .lib .skylarkinterface .SkylarkCallable ;
28
27
import com .google .devtools .build .lib .skylarkinterface .SkylarkModule ;
@@ -60,14 +59,13 @@ public class CheckoutPath implements Comparable<CheckoutPath>, StarlarkValue {
60
59
this .checkoutDir = Preconditions .checkNotNull (checkoutDir );
61
60
}
62
61
63
- private CheckoutPath create (Path path , Location location ) throws EvalException {
64
- return createWithCheckoutDir (path , checkoutDir , location );
62
+ private CheckoutPath create (Path path ) throws EvalException {
63
+ return createWithCheckoutDir (path , checkoutDir );
65
64
}
66
65
67
- static CheckoutPath createWithCheckoutDir (Path relative , Path checkoutDir ,
68
- Location location ) throws EvalException {
66
+ static CheckoutPath createWithCheckoutDir (Path relative , Path checkoutDir ) throws EvalException {
69
67
if (relative .isAbsolute ()) {
70
- throw new EvalException ( location , "Absolute paths are not allowed: " + relative );
68
+ throw Starlark . errorf ( "Absolute paths are not allowed: %s" , relative );
71
69
}
72
70
return new CheckoutPath (relative .normalize (), checkoutDir );
73
71
}
@@ -89,19 +87,16 @@ public String name() {
89
87
name = "parent" ,
90
88
doc = "Get the parent path" ,
91
89
structField = true ,
92
- allowReturnNones = true ,
93
- useLocation = true )
94
- public Object parent (Location location ) throws EvalException {
90
+ allowReturnNones = true )
91
+ public Object parent () throws EvalException {
95
92
Path parent = path .getParent ();
96
93
if (parent == null ) {
97
94
// nio equivalent of new_path("foo").parent returns null, but we want to be able to do
98
95
// foo.parent.resolve("bar"). While sibbling could be use for this, sometimes we'll need
99
96
// to return the parent folder and another function resolve a path based on that.
100
- return path .toString ().equals ("" )
101
- ? Starlark .NONE
102
- : create (path .getFileSystem ().getPath ("" ), location );
97
+ return path .toString ().equals ("" ) ? Starlark .NONE : create (path .getFileSystem ().getPath ("" ));
103
98
}
104
- return create (parent , location );
99
+ return create (parent );
105
100
}
106
101
107
102
@ SkylarkCallable (
@@ -115,9 +110,9 @@ public Object parent(Location location) throws EvalException {
115
110
name = "other" ,
116
111
type = CheckoutPath .class ,
117
112
doc = "The path to relativize against this path" ),
118
- }, useLocation = true )
119
- public CheckoutPath relativize (CheckoutPath other , Location location ) throws EvalException {
120
- return create (path .relativize (other .path ), location );
113
+ })
114
+ public CheckoutPath relativize (CheckoutPath other ) throws EvalException {
115
+ return create (path .relativize (other .path ));
121
116
}
122
117
123
118
@ SkylarkCallable (
@@ -130,15 +125,15 @@ public CheckoutPath relativize(CheckoutPath other, Location location) throws Eva
130
125
doc =
131
126
"Resolve the given path against this path. The parameter"
132
127
+ " can be a string or a Path." )
133
- }, useLocation = true )
134
- public CheckoutPath resolve (Object child , Location location ) throws EvalException {
128
+ })
129
+ public CheckoutPath resolve (Object child ) throws EvalException {
135
130
if (child instanceof String ) {
136
- return create (path .resolve ((String ) child ), location );
131
+ return create (path .resolve ((String ) child ));
137
132
} else if (child instanceof CheckoutPath ) {
138
- return create (path .resolve (((CheckoutPath ) child ).path ), location );
133
+ return create (path .resolve (((CheckoutPath ) child ).path ));
139
134
}
140
- throw new EvalException ( location ,
141
- "Cannot resolve children for type " + child .getClass ().getSimpleName () + ":" + child );
135
+ throw Starlark . errorf (
136
+ "Cannot resolve children for type %s: %s" , child .getClass ().getSimpleName (), child );
142
137
}
143
138
144
139
@ SkylarkCallable (
@@ -151,57 +146,54 @@ public CheckoutPath resolve(Object child, Location location) throws EvalExceptio
151
146
doc =
152
147
"Resolve the given path against this path. The parameter can be a string or"
153
148
+ " a Path." ),
154
- }, useLocation = true )
155
- public CheckoutPath resolveSibling (Object other , Location location ) throws EvalException {
149
+ })
150
+ public CheckoutPath resolveSibling (Object other ) throws EvalException {
156
151
if (other instanceof String ) {
157
- return create (path .resolveSibling ((String ) other ), location );
152
+ return create (path .resolveSibling ((String ) other ));
158
153
} else if (other instanceof CheckoutPath ) {
159
- return create (path .resolveSibling (((CheckoutPath ) other ).path ), location );
154
+ return create (path .resolveSibling (((CheckoutPath ) other ).path ));
160
155
}
161
- throw new EvalException ( location ,
162
- "Cannot resolve sibling for type " + other .getClass ().getSimpleName () + ": " + other );
156
+ throw Starlark . errorf (
157
+ "Cannot resolve sibling for type %s: %s" , other .getClass ().getSimpleName (), other );
163
158
}
164
159
165
160
@ SkylarkCallable (
166
161
name = "attr" ,
167
162
doc = "Get the file attributes, for example size." ,
168
- structField = true ,
169
- useLocation = true )
170
- public CheckoutPathAttributes attr (Location location ) throws EvalException {
163
+ structField = true )
164
+ public CheckoutPathAttributes attr () throws EvalException {
171
165
try {
172
166
return new CheckoutPathAttributes (path ,
173
167
Files .readAttributes (checkoutDir .resolve (path ), BasicFileAttributes .class ,
174
168
LinkOption .NOFOLLOW_LINKS ));
175
169
} catch (IOException e ) {
176
170
String msg = "Error getting attributes for " + path + ":" + e ;
177
171
logger .atSevere ().withCause (e ).log (msg );
178
- throw new EvalException ( location , msg ); // or IOException?
172
+ throw Starlark . errorf ( "%s" , msg ); // or IOException?
179
173
}
180
174
}
181
175
182
- @ SkylarkCallable (name = "read_symlink" , doc = "Read the symlink" , useLocation = true )
183
- public CheckoutPath readSymbolicLink (Location location ) throws EvalException {
176
+ @ SkylarkCallable (name = "read_symlink" , doc = "Read the symlink" )
177
+ public CheckoutPath readSymbolicLink () throws EvalException {
184
178
try {
185
179
Path symlinkPath = checkoutDir .resolve (path );
186
180
if (!Files .isSymbolicLink (symlinkPath )) {
187
- throw new EvalException ( location , String . format ("%s is not a symlink" , path ) );
181
+ throw Starlark . errorf ("%s is not a symlink" , path );
188
182
}
189
183
190
184
ResolvedSymlink resolvedSymlink =
191
185
FileUtil .resolveSymlink (Glob .ALL_FILES .relativeTo (checkoutDir ), symlinkPath );
192
186
if (!resolvedSymlink .isAllUnderRoot ()) {
193
- throw new EvalException (
194
- location ,
195
- String .format (
196
- "Symlink %s points to a file outside the checkout dir: %s" ,
197
- symlinkPath , resolvedSymlink .getRegularFile ()));
187
+ throw Starlark .errorf (
188
+ "Symlink %s points to a file outside the checkout dir: %s" ,
189
+ symlinkPath , resolvedSymlink .getRegularFile ());
198
190
}
199
191
200
- return create (checkoutDir .relativize (resolvedSymlink .getRegularFile ()), location );
192
+ return create (checkoutDir .relativize (resolvedSymlink .getRegularFile ()));
201
193
} catch (IOException e ) {
202
194
String msg = String .format ("Cannot resolve symlink %s: %s" , path , e );
203
195
logger .atSevere ().withCause (e ).log (msg );
204
- throw new EvalException ( null , msg , e );
196
+ throw Starlark . errorf ( "%s" , msg );
205
197
}
206
198
}
207
199
0 commit comments