Changeset 39

Show
Ignore:
Timestamp:
07/24/06 02:14:18 (6 years ago)
Author:
mloar2
Message:

Added several command-line options. Removed some dead code. Cleaned things up a bit.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wipt-get/src/Main.cs

    r36 r39  
    4747      public static void Main(string[] args) 
    4848      { 
    49         bool ignoretransforms = false; 
    50         bool ignorepatches = false; 
    51         bool peruser = false; 
    52         string command = ""; 
    53         string packages = ""; 
    54  
    55         foreach(string arg in args) 
    56         { 
    57           switch(arg.ToLower()) 
    58           { 
    59             case "--ignore-transforms": 
    60               ignoretransforms = true; 
    61             break; 
    62             case "--ignore-patches": 
    63               ignorepatches = true; 
    64             break; 
    65             case "--per-user": 
    66               peruser = true; 
     49        try 
     50        { 
     51          bool ignoretransforms = false; 
     52          bool ignorepatches = false; 
     53          bool peruser = false; 
     54          string targetdir = ""; 
     55          string command = ""; 
     56          string packages = ""; 
     57 
     58          foreach(string arg in args) 
     59          { 
     60            switch(arg.ToLower()) 
     61            { 
     62              case "--ignore-transforms": 
     63                ignoretransforms = true; 
     64              break; 
     65              case "--ignore-patches": 
     66                ignorepatches = true; 
     67              break; 
     68              case "--per-user": 
     69                peruser = true; 
     70              break; 
     71              default: 
     72                if(arg.ToLower().StartsWith("--target-dir")) 
     73                { 
     74                  string[] parts = arg.ToLower().Split('='); 
     75                  if(parts[1] != null) 
     76                    targetdir=parts[1]; 
     77                  else 
     78                  { 
     79                    Console.Error.WriteLine("You must specify a directory when using --target-dir"); 
     80                    Usage(); 
     81                    return; 
     82                  } 
     83                } 
     84                else if(command == "") 
     85                  command = arg.ToLower(); 
     86                else 
     87                  packages += arg + ","; 
     88              break; 
     89            } 
     90          } 
     91 
     92          if(command == "") 
     93          { 
     94            Console.Error.WriteLine("Error: no command specified"); 
     95            Usage(); 
     96            return; 
     97          } 
     98 
     99          if((command == "install" || command == "remove") && packages == "") 
     100          { 
     101            Console.Error.WriteLine("Error: no packages specified"); 
     102            Usage(); 
     103            return; 
     104          } 
     105 
     106          bool success = true; 
     107          switch(command.ToLower()) 
     108          { 
     109            case "install": 
     110              foreach(string package in packages.Split(',')) 
     111              { 
     112                success = Install(package, ignoretransforms, ignorepatches, peruser, targetdir) && success; 
     113              } 
     114            break; 
     115            case "remove": 
     116              Remove(packages.Split(',')); 
     117            break; 
     118            case "show": 
     119              List(); 
     120            break; 
     121            case "upgrade": 
     122              Upgrade(packages.Split(','), ignoretransforms, ignorepatches, peruser); 
     123            break; 
     124            case "dist-upgrade": 
     125              DistUpgrade(packages.Split(','), ignoretransforms, ignorepatches, peruser, targetdir); 
     126            break; 
     127            case "update": 
     128              Update(); 
    67129            break; 
    68130            default: 
    69               if(command == "") 
    70                 command = arg.ToLower(); 
    71               else 
    72                 packages += arg + ","; 
    73             break; 
    74           } 
    75         } 
    76  
    77         if(command == "") 
    78         { 
    79           Console.Error.WriteLine("Error: no command specified"); 
    80           Usage(); 
    81           return; 
    82         } 
    83  
    84         if((command == "install" || command == "remove") && packages == "") 
    85         { 
    86           Console.Error.WriteLine("Error: no packages specified"); 
    87           Usage(); 
    88           return; 
    89         } 
    90  
    91         bool success = true; 
    92         switch(command.ToLower()) 
    93         { 
    94           case "install": 
    95             foreach(string package in packages.Split(',')) 
    96             { 
    97               success = Install(package, ignoretransforms, ignorepatches, peruser) && success; 
    98             } 
    99           break; 
    100           case "remove": 
    101             Remove(packages.Split(',')); 
    102           break; 
    103           case "show": 
    104             List(); 
    105           break; 
    106           case "upgrade": 
    107             Upgrade(ignoretransforms, ignorepatches, peruser); 
    108           break; 
    109           case "dist-upgrade": 
    110             DistUpgrade(ignoretransforms, ignorepatches, peruser); 
    111           break; 
    112           case "update": 
    113             Update(); 
    114           break; 
    115           default: 
    116             Usage(); 
    117           break; 
    118       } 
    119     } 
    120  
    121     private static bool Install(string p, bool ignoretransforms, bool ignorepatches, bool peruser) 
     131              Usage(); 
     132            break; 
     133          } 
     134        } 
     135        catch(Exception e) 
     136        { 
     137          Console.Error.WriteLine( 
     138              "Wipt has encountered a serious problem and is unable to continue.  Please report this to SIGWin."); 
     139          Console.Error.Write(e.Message + e.StackTrace); 
     140        } 
     141      } 
     142 
     143    private static bool Install(string p, bool ignoretransforms, bool ignorepatches, bool peruser, string targetdir) 
    122144    { 
    123145      try 
     
    150172        string URL = ""; 
    151173        string properties = ""; 
    152         string targetdir = ""; 
    153174        Patch[] patches = null; 
    154         string temp; 
    155         if((temp = WiptConfig.GetTargetPath()) != null) 
    156         { 
    157           targetdir = temp; 
    158         } 
    159         if(!ignoretransforms && product.transforms != null) 
     175 
     176        if(targetdir == "") 
     177        { 
     178          string temp; 
     179          if((temp = WiptConfig.GetTargetPath()) != null) 
     180          { 
     181            targetdir = temp; 
     182          } 
     183        } 
     184        if(!ignoretransforms && product.transforms.Length > 0) 
    160185        { 
    161186          properties += "TRANSFORMS="; 
     
    170195        Guid productCode = Guid.Empty; 
    171196        patches = product.patches; 
    172         if(product.packages != null) 
    173         { 
    174           foreach(Package package in product.packages) 
    175           { 
    176             if(package.version == instVersion) 
    177             { 
    178               URL = package.URL; 
    179               productCode = package.productCode; 
    180               break; 
    181             } 
    182           } 
    183  
     197 
     198        foreach(Package package in product.packages) 
     199        { 
     200          if(package.version == instVersion) 
     201          { 
     202            URL = package.URL; 
     203            productCode = package.productCode; 
     204            break; 
     205          } 
    184206        } 
    185207 
     
    211233        } 
    212234 
    213         Console.Write("Installing product "+ product.name + "... "); 
     235        Console.Write("Installing "+ product.name + "... "); 
    214236 
    215237        ApplicationDatabase.setProgressHandler( 
     
    222244        Console.WriteLine(ApplicationDatabase.getErrorMessage(ret)); 
    223245 
     246 
     247        // Hackish way to prevent reapplication of patches 
     248        foreach(Patch x in patches) 
     249        { 
     250          if(IsPatchApplied(x, productCode)) 
     251          { 
     252            x.productCodes = new Guid[1]{Guid.Empty}; 
     253          } 
     254        } 
    224255 
    225256        if((ret != 0 && ret != 3010) || (ignorepatches || !ApplyPatches(patches, productCode))) 
     
    262293          if(instVersion == null ? IsInstalled(product.name) : IsInstalled(product.name, instVersion, instVersion)) 
    263294          { 
    264             Console.Write("Removing product "+ product.name + "... "); 
     295            Console.Write("Removing "+ product.name + "... "); 
    265296 
    266297            ApplicationDatabase.setProgressHandler( 
     
    287318    { 
    288319      bool success = true; 
    289       if(patches != null
     320      if(patches != null && patches.Length > 0
    290321      { 
    291322        foreach(Patch patch in patches) 
    292323        { 
    293           if(patch.productCodes == null
     324          if(patch.productCodes.Length == 0
    294325          { 
    295326            patch.productCodes = new Guid[1]; 
     
    314345    } 
    315346 
     347    public static bool IsPatchApplied(Patch patch, Guid productCode) 
     348    { 
     349      int k = 0; 
     350 
     351      Guid code; 
     352      while((code = ApplicationDatabase.getInstalledPatches(productCode, k++)) != Guid.Empty) 
     353      { 
     354        if(patch.patchCode == code) 
     355          return true; 
     356      } 
     357       
     358      return false; 
     359    } 
     360 
    316361    public static void Usage() 
    317362    { 
    318363      string usage = @" 
    319         Usage: wipt-get [options] <command> <product>[=version][, <product>[=version], ...] 
     364        Usage: wipt-get [options] <command> <product>[=version][, <product>[=version], ...] 
    320365        OPTIONS 
    321366        --ignore-patches            Don't apply patches listed in repository 
    322367        --ignore-transforms         Don't apply transforms listed in repository 
    323368        --per-user                  Perform a per-user install 
     369        --target-dir=DIR            Override the TARGETDIR setting 
    324370 
    325371        COMMANDS 
     
    363409    } 
    364410 
    365     public static void Upgrade(bool ignoretransforms, bool ignorepatches, bool peruser) 
    366     { 
    367       Product[] list = Library.GetAll(); 
     411    public static void Upgrade(string[] products, bool ignoretransforms, bool ignorepatches, bool peruser) 
     412    { 
     413      Product[] list = new Product[0]; 
     414      if(products.Length == 1 && products[0] == "") 
     415      { 
     416        list = Library.GetAll(); 
     417      } 
     418      else 
     419      { 
     420        foreach(string product in products) 
     421        { 
     422          if(product != "") 
     423          { 
     424            Product p = Library.GetProduct(product); 
     425            if(p == null) 
     426            { 
     427              Console.Error.WriteLine("No such product {0}", product); 
     428            } 
     429            else 
     430            { 
     431              Product[] nl = new Product[list.Length + 1]; 
     432              Array.Copy(list, nl, list.Length); 
     433              nl[list.Length] = p; 
     434              list = nl; 
     435            } 
     436          } 
     437        } 
     438      } 
    368439      foreach(Product p in list) 
    369440      { 
     
    379450              if(new Version(ApplicationDatabase.getInstalledVersion(prod)) < g.version) 
    380451              { 
    381                 Install(p.name + "=" + g.version.ToString(), ignoretransforms, ignorepatches, peruser); 
     452                Install(p.name + "=" + g.version.ToString(), ignoretransforms, ignorepatches, peruser, ""); 
    382453              } 
    383454              else if(!ignorepatches) 
    384455              { 
     456                foreach(Patch x in p.patches) 
     457                { 
     458                  if(IsPatchApplied(x, g.productCode)) 
     459                  { 
     460                    x.productCodes = new Guid[1]{Guid.Empty}; 
     461                  } 
     462                } 
     463 
    385464                ApplyPatches(p.patches, prod); 
    386465              } 
     
    394473    } 
    395474 
    396     public static void DistUpgrade(bool ignoretransforms, bool ignorepatches, bool peruser) 
     475    public static void DistUpgrade(string[] products, bool ignoretransforms, bool ignorepatches, bool peruser, 
     476        string targetdir) 
    397477    { 
    398478      try 
    399479      { 
     480        Product[] list = new Product[0]; 
     481        if(products.Length == 1 && products[0] == "") 
     482        { 
     483          list = Library.GetAll(); 
     484        } 
     485        else 
     486        { 
     487          foreach(string product in products) 
     488          { 
     489            if(product != "") 
     490            { 
     491              Product p = Library.GetProduct(product); 
     492              if(p == null) 
     493              { 
     494                Console.Error.WriteLine("No such product {0}", product); 
     495              } 
     496              else 
     497              { 
     498                Product[] nl = new Product[list.Length + 1]; 
     499                Array.Copy(list, nl, list.Length); 
     500                nl[list.Length] = p; 
     501                list = nl; 
     502              } 
     503            } 
     504          } 
     505        } 
     506 
     507        foreach(Product p in list) 
     508        { 
     509          if(IsInstalled(p.name) && !IsInstalled(p.name, p.stableVersion, new Version("99.99.9999"))) 
     510          { 
     511            Install(p.name + "=" + p.stableVersion.ToString(), ignoretransforms, ignorepatches, peruser, targetdir); 
     512          } 
     513        } 
     514      } 
     515      catch(WiptException e) 
     516      { 
     517        Console.Error.Write(e.Message); 
     518      } 
     519    } 
     520 
     521    public static void List() 
     522    { 
     523      try 
     524      { 
    400525        Product[] list = Library.GetAll(); 
    401         foreach(Product p in list) 
    402         { 
    403           if(IsInstalled(p.name) && !IsInstalled(p.name, p.stableVersion, new Version("99.99.9999"))) 
    404           { 
    405             Install(p.name + "=" + p.stableVersion.ToString(), ignoretransforms, ignorepatches, peruser); 
    406           } 
     526        if(list.Length > 0) 
     527        { 
     528          Array.Sort(list); 
     529          foreach(Product p in list) 
     530          { 
     531            Console.WriteLine("\n" + p.name); 
     532            if(p.packages.Length > 0) 
     533            { 
     534              Array.Sort(p.packages); 
     535              foreach(Package k in p.packages) 
     536              { 
     537                string installstring=""; 
     538                if(p.stableVersion == k.version) 
     539                  installstring = "(stable)"; 
     540                if(ApplicationDatabase.getProductState(k.productCode) 
     541                    == InstallState.Default) 
     542                  installstring += "(installed)"; 
     543                Console.WriteLine("  v{0} {1}", k.version.ToString(), installstring); 
     544              } 
     545            } 
     546            else 
     547            { 
     548              Console.WriteLine("  [No packages found]"); 
     549            } 
     550          } 
     551        } 
     552        else 
     553        { 
     554          Console.Error.WriteLine("No packages found in database."); 
    407555        } 
    408556      } 
    409557      catch(WiptException e) 
    410558      { 
    411         Console.Error.Write(e.Message); 
    412       } 
    413     } 
    414  
    415     public static void List() 
    416     { 
    417       try 
    418       { 
    419         Product[] list = Library.GetAll(); 
    420         Array.Sort(list); 
    421         foreach(Product p in list) 
    422         { 
    423           Console.WriteLine("\n" + p.name); 
    424           Array.Sort(p.packages); 
    425           foreach(Package k in p.packages) 
    426           { 
    427             string installstring=""; 
    428             if(p.stableVersion == k.version) 
    429               installstring = "(stable)"; 
    430             if(ApplicationDatabase.getProductState(k.productCode) 
    431                 == InstallState.Default) 
    432               installstring += "(installed)"; 
    433             Console.WriteLine("\tv{0} {1}", k.version.ToString(), installstring); 
    434           } 
    435         } 
    436       } 
    437       catch(WiptException e) 
    438       { 
    439559        Console.WriteLine(e.Message); 
    440560      } 
     
    443563    private static Guid GetVersionProductCode(Guid upgradecode, Version version) 
    444564    { 
    445     Guid ret; 
    446     int i = 0; 
    447     while((ret = ApplicationDatabase.findProductByUpgradeCode(upgradecode, i)) != Guid.Empty) 
    448     { 
    449       if(version == null || version == new Version(ApplicationDatabase.getInstalledVersion(ret))) 
    450       { 
    451         return ret; 
     565      Guid ret; 
     566      int i = 0; 
     567      while((ret = ApplicationDatabase.findProductByUpgradeCode(upgradecode, i)) != Guid.Empty) 
     568      { 
     569        if(version == null || version == new Version(ApplicationDatabase.getInstalledVersion(ret))) 
     570        { 
     571          return ret; 
     572          } 
     573          else 
     574          { 
     575            i++; 
     576          } 
     577        } 
     578 
     579      return Guid.Empty; 
     580    } 
     581 
     582    private static bool IsInstalled(string product) 
     583    { 
     584      bool installed = false; 
     585      Product p = Library.GetProduct(product); 
     586 
     587      if(p == null) 
     588      { 
     589        installed = false; 
    452590      } 
    453591      else 
    454592      { 
    455         i++; 
    456       } 
    457     } 
    458  
    459     return Guid.Empty; 
    460   } 
    461  
    462   private static bool IsInstalled(string product) 
    463   { 
    464     bool installed = false; 
    465     Object obj = Library.GetProduct(product); 
    466  
    467     if(obj == null || obj is Patch) 
    468     { 
    469       installed = false; 
    470     } 
    471     else if(obj is Product) 
    472     { 
    473       Product p = (Product)obj; 
     593        int i = 0; 
     594        Guid productCode; 
     595        while((productCode = ApplicationDatabase.findProductByUpgradeCode(p.upgradeCode, i++)) != Guid.Empty) 
     596        { 
     597          InstallState state = ApplicationDatabase.getProductState(productCode); 
     598          if(!(state ==  InstallState.Removed || state == InstallState.Absent || state == InstallState.Unknown)) 
     599          { 
     600            installed = true; 
     601          } 
     602        } 
     603      } 
     604 
     605      return installed; 
     606    } 
     607 
     608    private static bool IsInstalled(string product, Version minVersion, Version maxVersion) 
     609    { 
     610      Product p = Library.GetProduct(product); 
     611      if(p == null) 
     612      { 
     613        return false; 
     614      } 
    474615 
    475616      int i = 0; 
    476617      Guid productCode; 
    477       while((productCode = ApplicationDatabase.findProductByUpgradeCode(p.upgradeCode, i++)) != Guid.Empty) 
    478       { 
    479         InstallState state = ApplicationDatabase.getProductState(productCode); 
    480         if(!(state ==  InstallState.Removed || state == InstallState.Absent || state == InstallState.Unknown)) 
    481         { 
    482           installed = true; 
    483         } 
    484       } 
    485     } 
    486  
    487     return installed; 
    488   } 
    489  
    490   private static bool IsInstalled(string product, Version minVersion, Version maxVersion) 
    491   { 
    492     Object obj = Library.GetProduct(product); 
    493     if(obj == null || !(obj is Product)) 
    494     { 
    495       return false; 
    496     } 
    497  
    498     Product p = (Product)obj; 
    499  
    500     int i = 0; 
    501     Guid productCode; 
    502618      while((productCode = ApplicationDatabase.findProductByUpgradeCode(p.upgradeCode, i++)) != Guid.Empty) 
    503619      {