@@ -46,42 +46,12 @@ def extract_from_archive(self, repodir, files, outdir):
4646
4747 shutil .copy2 (src , outdir )
4848
49-
50- class ObsCpio (BaseArchive ):
51- def create_archive (self , scm_object , ** kwargs ):
52- """Create an OBS cpio archive of repodir in destination directory.
49+ def filter_files (self , filelist , topdir , args ):
50+ """
51+ Filter filelist by exclude/include parameters
5352 """
54- basename = kwargs ['basename' ]
55- dstname = kwargs ['dstname' ]
56- version = kwargs ['version' ]
57- args = kwargs ['cli' ]
58- commit = scm_object .get_current_commit ()
5953 package_metadata = args .package_meta
6054
61- (workdir , topdir ) = os .path .split (scm_object .arch_dir )
62- extension = 'obscpio'
63-
64- cwd = os .getcwd ()
65- os .chdir (workdir )
66-
67- archivefilename = os .path .join (args .outdir , dstname + '.' + extension )
68- archivefile = open (archivefilename , "w" )
69-
70- # detect reproducible support
71- params = ['cpio' , '--create' , '--format=newc' , '--owner' , '0:0' ]
72- chkcmd = "cpio --create --format=newc --reproducible "
73- chkcmd += "</dev/null >/dev/null 2>&1"
74- if os .system (chkcmd ) == 0 :
75- params .append ('--reproducible' )
76-
77- proc = subprocess .Popen (
78- params ,
79- shell = False ,
80- stdin = subprocess .PIPE ,
81- stdout = archivefile ,
82- stderr = subprocess .STDOUT
83- )
84-
8555 # transform glob patterns to regular expressions
8656 includes = ''
8757 excludes = r'$.'
@@ -96,7 +66,7 @@ def create_archive(self, scm_object, **kwargs):
9666
9767 # add topdir without filtering for now
9868 cpiolist = []
99- for root , dirs , files in os . walk ( topdir , topdown = False ) :
69+ for root , dirs , files in filelist :
10070 # excludes
10171 dirs [:] = [os .path .join (root , d ) for d in dirs ]
10272 dirs [:] = [d for d in dirs if not re .match (excludes , d )]
@@ -114,9 +84,44 @@ def create_archive(self, scm_object, **kwargs):
11484 for name in files :
11585 if not METADATA_PATTERN .match (name ) or package_metadata :
11686 cpiolist .append (name )
87+ return sorted (cpiolist )
11788
89+ class ObsCpio (BaseArchive ):
90+ def create_archive (self , scm_object , ** kwargs ):
91+ """Create an OBS cpio archive of repodir in destination directory.
92+ """
93+ basename = kwargs ['basename' ]
94+ dstname = kwargs ['dstname' ]
95+ version = kwargs ['version' ]
96+ args = kwargs ['cli' ]
97+ commit = scm_object .get_current_commit ()
98+
99+ (workdir , topdir ) = os .path .split (scm_object .arch_dir )
100+ extension = 'obscpio'
101+
102+ cwd = os .getcwd ()
103+ os .chdir (workdir )
104+
105+ archivefilename = os .path .join (args .outdir , dstname + '.' + extension )
106+ archivefile = open (archivefilename , "w" )
107+
108+ # detect reproducible support
109+ params = ['cpio' , '--create' , '--format=newc' , '--owner' , '0:0' ]
110+ chkcmd = "cpio --create --format=newc --reproducible "
111+ chkcmd += "</dev/null >/dev/null 2>&1"
112+ if os .system (chkcmd ) == 0 :
113+ params .append ('--reproducible' )
114+
115+ proc = subprocess .Popen (
116+ params ,
117+ shell = False ,
118+ stdin = subprocess .PIPE ,
119+ stdout = archivefile ,
120+ stderr = subprocess .STDOUT
121+ )
122+ filelist = os .walk (topdir , topdown = False )
118123 tstamp = self .helpers .get_timestamp (scm_object , args , topdir )
119- for name in sorted ( cpiolist ):
124+ for name in self . filter_files ( filelist , topdir , args ):
120125 try :
121126 os .utime (name , (tstamp , tstamp ), follow_symlinks = False )
122127 except OSError :
@@ -186,24 +191,6 @@ def create_archive(self, scm_object, **kwargs):
186191 pat = fnmatch .translate (os .path .join (topdir , exc ))
187192 excl_patterns .append (re .compile (pat ))
188193
189- def tar_exclude (filename ):
190- """
191- Exclude (return True) or add (return False) file to tar achive.
192- """
193- if not package_metadata and METADATA_PATTERN .match (filename ):
194- return True
195-
196- if incl_patterns :
197- for pat in incl_patterns :
198- if pat .match (filename ):
199- return False
200- return True
201-
202- for pat in excl_patterns :
203- if pat .match (filename ):
204- return True
205- return False
206-
207194 def reset (tarinfo ):
208195 """Python 2.7 only: reset uid/gid to 0/0 (root)."""
209196 tarinfo .uid = tarinfo .gid = 0
@@ -212,31 +199,32 @@ def reset(tarinfo):
212199 tarinfo .mtime = timestamp
213200 return tarinfo
214201
215- def tar_filter (tarinfo ):
216- if tar_exclude (tarinfo .name ):
217- return None
218-
219- return reset (tarinfo )
220-
221202 cwd = os .getcwd ()
222203 os .chdir (workdir )
223204 enc = locale .getpreferredencoding ()
224205
225206 out_file = os .path .join (outdir , dstname + '.' + extension )
207+ filelist = os .walk (topdir , topdown = False )
208+
209+ files_added = dict ()
226210
227211 with tarfile .open (out_file , "w" , encoding = enc ) as tar :
228212 try :
229213 tar .add (topdir , recursive = False , filter = reset )
230214 except TypeError :
231215 # Python 2.6 compatibility
232216 tar .add (topdir , recursive = False )
233- for entry in map (lambda x : os .path .join (topdir , x ),
234- sorted (os .listdir (topdir ))):
235- try :
236- tar .add (entry , filter = tar_filter )
237- except TypeError :
238- # Python 2.6 compatibility
239- tar .add (entry , exclude = tar_exclude )
217+ for entry in self .filter_files (filelist , topdir , args ):
218+ logging .debug ("Filtered file: %s" , entry )
219+ if not files_added .get (entry , False ):
220+ logging .debug ("Adding filtered file: %s" , entry )
221+ try :
222+ tar .add (entry , recursive = False , filter = reset )
223+ except TypeError :
224+ # Python 2.6 compatibility
225+ tar .add (entry , exclude = tar_exclude )
226+ files_added [entry ] = True
227+ logging .debug ("Added filtered file: %s" , entry )
240228
241229 self .archivefile = tar .name
242230
0 commit comments