4040 */
4141package org .graalvm .python .embedding .tools .vfs ;
4242
43+ import java .io .Serial ;
4344import org .graalvm .python .embedding .tools .exec .BuildToolLog ;
4445import org .graalvm .python .embedding .tools .exec .BuildToolLog .CollectOutputLog ;
4546import org .graalvm .python .embedding .tools .exec .GraalPyRunner ;
@@ -317,19 +318,17 @@ static InstalledPackages fromVenv(Path venvDirectory) throws IOException {
317318 return new InstalledPackages (venvDirectory , installed , pkgs );
318319 }
319320
320- List < String > freeze (BuildToolLog log ) throws IOException {
321+ void freeze (BuildToolLog log ) throws IOException {
321322 CollectOutputLog collectOutputLog = new CollectOutputLog (log );
322323 runPip (venvDirectory , "freeze" , collectOutputLog , "--local" );
323324 packages = new ArrayList <>(collectOutputLog .getOutput ());
324325
325326 String toWrite = "# Generated by GraalPy Maven or Gradle plugin using pip freeze\n "
326327 + "# This file is used by GraalPy VirtualFileSystem\n " + String .join ("\n " , packages );
327- Files .write (installedFile , toWrite . getBytes ( StandardCharsets . UTF_8 ) , StandardOpenOption .CREATE ,
328+ Files .writeString (installedFile , toWrite , StandardOpenOption .CREATE ,
328329 StandardOpenOption .TRUNCATE_EXISTING );
329330
330331 logDebug (log , packages , "VFSUtils venv packages after install %s:" , installedFile );
331-
332- return packages ;
333332 }
334333 }
335334
@@ -365,12 +364,10 @@ static VenvContents fromVenv(Path venvDirectory) throws IOException {
365364 if (lines .get (0 ).startsWith ("version=" )) {
366365 // this was created with version >= 25
367366 Map <String , String > m = new HashMap <>();
368- Iterator <String > it = lines .iterator ();
369- while (it .hasNext ()) {
370- String l = it .next ();
371- int idx = l .indexOf ("=" );
372- m .put (l .substring (0 , idx ), l .substring (idx + 1 ));
373- }
367+ for (String l : lines ) {
368+ int idx = l .indexOf ("=" );
369+ m .put (l .substring (0 , idx ), l .substring (idx + 1 ));
370+ }
374371 String graalPyVersion = m .get (KEY_VERSION );
375372 String platform = m .get (KEY_PLATFORM );
376373 String packagesLine = m .get (KEY_PACKAGES );
@@ -516,7 +513,8 @@ private static List<String> getHeaderList(String lockFileHeader) {
516513 }
517514
518515 public static final class PackagesChangedException extends Exception {
519- private static final long serialVersionUID = 9162516912727973035L ;
516+ @ Serial
517+ private static final long serialVersionUID = 9162516912727973035L ;
520518
521519 private final transient List <String > pluginPackages ;
522520 private final transient List <String > lockFilePackages ;
@@ -716,15 +714,16 @@ private static void logPackages(List<String> packages, Path lockFile, BuildToolL
716714 }
717715 }
718716
719- private static List <String > readPackagesFromFile (Path file ) throws IOException {
720- return Files .readAllLines (file ).stream ().filter ((s ) -> {
721- if (s == null ) {
722- return false ;
723- }
724- String l = s .trim ();
725- return !l .startsWith ("#" ) && !s .isEmpty ();
726- }).toList ();
727- }
717+ private static List <String > readPackagesFromFile (Path file ) throws IOException {
718+ return Files .readAllLines (file ).stream ()
719+ .map (String ::trim )
720+ .filter (line -> !line .isEmpty () && !line .startsWith ("#" ))
721+ .toList ();
722+ }
723+
724+ public static List <String > requirementsPackages (Path requirementsFile ) throws IOException {
725+ return Files .exists (requirementsFile ) ? readPackagesFromFile (requirementsFile ) : Collections .emptyList ();
726+ }
728727
729728 private static VenvContents ensureVenv (Path venvDirectory , String graalPyVersion , Launcher launcher ,
730729 BuildToolLog log ) throws IOException {
@@ -783,7 +782,7 @@ private static boolean install(Path venvDirectory, List<String> newPackages, Ven
783782 private static void missingLockFileWarning (Path venvDirectory , List <String > newPackages ,
784783 String missingLockFileWarning , BuildToolLog log ) throws IOException {
785784 if (missingLockFileWarning != null && !Boolean .getBoolean ("graalpy.vfs.skipMissingLockFileWarning" )) {
786- if (!newPackages .containsAll (InstalledPackages .fromVenv (venvDirectory ).packages )) {
785+ if (!new HashSet <>( newPackages ) .containsAll (InstalledPackages .fromVenv (venvDirectory ).packages )) {
787786 if (log .isWarningEnabled ()) {
788787 String txt = missingLockFileWarning + "\n " ;
789788 for (String t : txt .split ("\n " )) {
@@ -800,7 +799,7 @@ private static void missingLockFileWarning(Path venvDirectory, List<String> newP
800799 private static void checkPluginPackagesInLockFile (List <String > pluginPackages , LockFile lockFile )
801800 throws PackagesChangedException {
802801 if (pluginPackages .size () != lockFile .inputPackages .size ()
803- || !pluginPackages .containsAll (lockFile .inputPackages )) {
802+ || !new HashSet <>( pluginPackages ) .containsAll (lockFile .inputPackages )) {
804803 throw new PackagesChangedException (new ArrayList <>(pluginPackages ),
805804 new ArrayList <>(lockFile .inputPackages ));
806805 }
@@ -928,7 +927,7 @@ with open(pyvenvcfg, 'w', encoding='utf-8') as f:
928927 private static boolean installWantedPackages (Path venvDirectory , List <String > packages ,
929928 List <String > installedPackages , BuildToolLog log ) throws IOException {
930929 Set <String > pkgsToInstall = new HashSet <>(packages );
931- pkgsToInstall . removeAll ( installedPackages );
930+ installedPackages . forEach ( pkgsToInstall :: remove );
932931 if (pkgsToInstall .isEmpty ()) {
933932 return false ;
934933 }
@@ -948,7 +947,7 @@ private static boolean deleteUnwantedPackages(Path venvDirectory, List<String> p
948947 }
949948 args .add (0 , "-y" );
950949
951- runPip (venvDirectory , "uninstall" , log , args .toArray (new String [args . size () ]));
950+ runPip (venvDirectory , "uninstall" , log , args .toArray (new String [0 ]));
952951 return true ;
953952 }
954953
@@ -980,12 +979,11 @@ private static void runVenvBin(Path venvDirectory, String bin, BuildToolLog log,
980979
981980 private static List <String > trim (List <String > l ) {
982981 Iterator <String > it = l .iterator ();
983- while (it .hasNext ()) {
984- String p = it .next ();
985- if (p == null || p .trim ().isEmpty ()) {
986- it .remove ();
987- }
988- }
982+ for (String s : l ) {
983+ if (s == null || s .trim ().isEmpty ()) {
984+ it .remove ();
985+ }
986+ }
989987 return l ;
990988 }
991989
@@ -1000,9 +998,9 @@ private static void info(BuildToolLog log, String txt, Object... args) {
1000998 }
1001999 }
10021000
1003- private static void lifecycle (BuildToolLog log , String txt , Object ... args ) {
1001+ private static void lifecycle (BuildToolLog log , Object ... args ) {
10041002 if (log .isLifecycleEnabled ()) {
1005- log .lifecycle (String .format (txt , args ));
1003+ log .lifecycle (String .format ("Created GraalPy lock file: %s" , args ));
10061004 }
10071005 }
10081006
0 commit comments