@sasjs/core 4.39.0 → 4.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/base/mp_dirlist.sas +16 -0
- package/base/mp_ds2md.sas +2 -2
- package/base/mp_hashdataset.sas +4 -3
- package/base/mp_hashdirectory.sas +162 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -237,6 +237,7 @@ If you find this library useful, please leave a [star](https://github.com/sasjs/
|
|
|
237
237
|
|
|
238
238
|
The following repositories are also worth checking out:
|
|
239
239
|
|
|
240
|
+
* [xieliaing/SAS](https://github.com/xieliaing/SAS)
|
|
240
241
|
* [SASJedi/sas-macros](https://github.com/SASJedi/sas-macros)
|
|
241
242
|
* [chris-swenson/sasmacros](https://github.com/chris-swenson/sasmacros)
|
|
242
243
|
* [greg-wotton/sas-programs](https://github.com/greg-wootton/sas-programs)
|
package/base/mp_dirlist.sas
CHANGED
|
@@ -27,6 +27,9 @@
|
|
|
27
27
|
@param [in] maxdepth= (0) Set to a positive integer to indicate the level of
|
|
28
28
|
subdirectory scan recursion - eg 3, to go `./3/levels/deep`. For unlimited
|
|
29
29
|
recursion, set to MAX.
|
|
30
|
+
@param [in] showparent= (NO) By default, the initial parent directory is not
|
|
31
|
+
part of the results. Set to YES to include it. For this record only,
|
|
32
|
+
directory=filepath.
|
|
30
33
|
@param [out] outds= (work.mp_dirlist) The output dataset to create
|
|
31
34
|
@param [out] getattrs= (NO) If getattrs=YES then the doptname / foptname
|
|
32
35
|
functions are used to scan all properties - any characters that are not
|
|
@@ -63,6 +66,7 @@
|
|
|
63
66
|
, fref=0
|
|
64
67
|
, outds=work.mp_dirlist
|
|
65
68
|
, getattrs=NO
|
|
69
|
+
, showparent=NO
|
|
66
70
|
, maxdepth=0
|
|
67
71
|
, level=0 /* The level of recursion to perform. For internal use only. */
|
|
68
72
|
)/*/STORE SOURCE*/;
|
|
@@ -145,6 +149,15 @@ data &out_ds(compress=no
|
|
|
145
149
|
output;
|
|
146
150
|
end;
|
|
147
151
|
rc = dclose(did);
|
|
152
|
+
%if &showparent=YES and &level=0 %then %do;
|
|
153
|
+
filepath=directory;
|
|
154
|
+
file_or_folder='folder';
|
|
155
|
+
ext='';
|
|
156
|
+
filename=scan(directory,-1,'/\');
|
|
157
|
+
msg='';
|
|
158
|
+
level=&level;
|
|
159
|
+
output;
|
|
160
|
+
%end;
|
|
148
161
|
stop;
|
|
149
162
|
run;
|
|
150
163
|
|
|
@@ -232,6 +245,9 @@ run;
|
|
|
232
245
|
data _null_;
|
|
233
246
|
set &out_ds;
|
|
234
247
|
where file_or_folder='folder';
|
|
248
|
+
%if &showparent=YES and &level=0 %then %do;
|
|
249
|
+
if filepath ne directory;
|
|
250
|
+
%end;
|
|
235
251
|
length code $10000;
|
|
236
252
|
code=cats('%nrstr(%mp_dirlist(path=',filepath,",outds=&outds"
|
|
237
253
|
,",getattrs=&getattrs,level=%eval(&level+1),maxdepth=&maxdepth))");
|
package/base/mp_ds2md.sas
CHANGED
|
@@ -92,7 +92,7 @@ data _null_;
|
|
|
92
92
|
run;
|
|
93
93
|
|
|
94
94
|
%if %upcase(&showlog)=YES %then %do;
|
|
95
|
-
options ps=max;
|
|
95
|
+
options ps=max lrecl=max;
|
|
96
96
|
data _null_;
|
|
97
97
|
infile &outref;
|
|
98
98
|
input;
|
|
@@ -100,4 +100,4 @@ run;
|
|
|
100
100
|
run;
|
|
101
101
|
%end;
|
|
102
102
|
|
|
103
|
-
%mend mp_ds2md;
|
|
103
|
+
%mend mp_ds2md;
|
package/base/mp_hashdataset.sas
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
put hashkey=;
|
|
12
12
|
run;
|
|
13
13
|
|
|
14
|
-

|
|
15
15
|
|
|
16
16
|
<h4> SAS Macros </h4>
|
|
17
17
|
@li mf_getattrn.sas
|
|
@@ -21,11 +21,12 @@
|
|
|
21
21
|
|
|
22
22
|
<h4> Related Files </h4>
|
|
23
23
|
@li mp_hashdataset.test.sas
|
|
24
|
+
@li mp_hashdirectory.sas
|
|
24
25
|
|
|
25
26
|
@param [in] libds dataset to hash
|
|
26
27
|
@param [in] salt= Provide a salt (could be, for instance, the dataset name)
|
|
27
|
-
@param [in] iftrue= A condition under which the macro should be executed
|
|
28
|
-
@param [out] outds= (work.
|
|
28
|
+
@param [in] iftrue= (1=1) A condition under which the macro should be executed
|
|
29
|
+
@param [out] outds= (work._data_) The output dataset to create. This
|
|
29
30
|
will contain one column (hashkey) with one observation (a $hex32.
|
|
30
31
|
representation of the input hash)
|
|
31
32
|
|hashkey:$32.|
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Returns a unique hash for each file in a directory
|
|
4
|
+
@details Hashes each file in each directory, and then hashes the hashes to
|
|
5
|
+
create a hash for each directory also.
|
|
6
|
+
|
|
7
|
+
This makes use of the new `hashing_file()` and `hashing` functions, available
|
|
8
|
+
since 9.4m6. Interestingly, these can even be used in pure macro, eg:
|
|
9
|
+
|
|
10
|
+
%put %sysfunc(hashing_file(md5,/path/to/file.blob,0));
|
|
11
|
+
|
|
12
|
+
Actual usage:
|
|
13
|
+
|
|
14
|
+
%let fpath=/some/directory;
|
|
15
|
+
|
|
16
|
+
%mp_hashdirectory(&fpath,outds=myhash,maxdepth=2)
|
|
17
|
+
|
|
18
|
+
data _null_;
|
|
19
|
+
set work.myhash;
|
|
20
|
+
put (_all_)(=);
|
|
21
|
+
run;
|
|
22
|
+
|
|
23
|
+
Whilst files are hashed in their entirety, the logic for creating a folder
|
|
24
|
+
hash is as follows:
|
|
25
|
+
|
|
26
|
+
@li Sort the files by filename (case sensitive, uppercase then lower)
|
|
27
|
+
@li Take the first 100 hashes, concatenate and hash
|
|
28
|
+
@li Concatenate this hash with another 100 hashes and hash again
|
|
29
|
+
@li Continue until the end of the folder. This is the folder hash
|
|
30
|
+
@li If a folder contains other folders, start from the bottom of the tree -
|
|
31
|
+
the folder hashes cascade upwards so you know immediately if there is a
|
|
32
|
+
change in a sub/sub directory
|
|
33
|
+
@li If the folder has no content (empty) then it is ignored. No hash created.
|
|
34
|
+
@li If the file is empty, it is also ignored / no hash created.
|
|
35
|
+
|
|
36
|
+
<h4> SAS Macros </h4>
|
|
37
|
+
@li mp_dirlist.sas
|
|
38
|
+
|
|
39
|
+
<h4> Related Files </h4>
|
|
40
|
+
@li mp_hashdataset.sas
|
|
41
|
+
@li mp_hashdirectory.test.sas
|
|
42
|
+
@li mp_md5.sas
|
|
43
|
+
|
|
44
|
+
@param [in] inloc Full filepath of the file to be hashed (unquoted)
|
|
45
|
+
@param [in] iftrue= (1=1) A condition under which the macro should be executed
|
|
46
|
+
@param [in] maxdepth= (0) Set to a positive integer to indicate the level of
|
|
47
|
+
subdirectory scan recursion - eg 3, to go `./3/levels/deep`. For unlimited
|
|
48
|
+
recursion, set to MAX.
|
|
49
|
+
@param [in] method= (MD5) the hashing method to use. Available options:
|
|
50
|
+
@li MD5
|
|
51
|
+
@li SH1
|
|
52
|
+
@li SHA256
|
|
53
|
+
@li SHA384
|
|
54
|
+
@li SHA512
|
|
55
|
+
@li CRC32
|
|
56
|
+
@param [out] outds= (work.mp_hashdirectory) The output dataset. Contains:
|
|
57
|
+
@li directory - the parent folder
|
|
58
|
+
@li file_hash - the hash output
|
|
59
|
+
@li hash_duration - how long the hash took (first hash always takes longer)
|
|
60
|
+
@li file_path - /full/path/to/each/file.ext
|
|
61
|
+
@li file_or_folder - contains either "file" or "folder"
|
|
62
|
+
@li level - the depth of the directory (top level is 0)
|
|
63
|
+
|
|
64
|
+
@version 9.4m6
|
|
65
|
+
@author Allan Bowe
|
|
66
|
+
**/
|
|
67
|
+
|
|
68
|
+
%macro mp_hashdirectory(inloc,
|
|
69
|
+
outds=work.mp_hashdirectory,
|
|
70
|
+
method=MD5,
|
|
71
|
+
maxdepth=0,
|
|
72
|
+
iftrue=%str(1=1)
|
|
73
|
+
)/*/STORE SOURCE*/;
|
|
74
|
+
|
|
75
|
+
%local curlevel tempds ;
|
|
76
|
+
|
|
77
|
+
%if not(%eval(%unquote(&iftrue))) %then %return;
|
|
78
|
+
|
|
79
|
+
/* get the directory listing */
|
|
80
|
+
%mp_dirlist(path=&inloc, outds=&outds, maxdepth=&maxdepth, showparent=YES)
|
|
81
|
+
|
|
82
|
+
/* create the hashes */
|
|
83
|
+
data &outds;
|
|
84
|
+
set &outds (rename=(filepath=file_path));
|
|
85
|
+
length FILE_HASH $32 HASH_DURATION 8;
|
|
86
|
+
keep directory file_hash hash_duration file_path file_or_folder level;
|
|
87
|
+
|
|
88
|
+
ts=datetime();
|
|
89
|
+
if file_or_folder='file' then do;
|
|
90
|
+
/* if file is empty, hashing_file will break - so ignore / delete */
|
|
91
|
+
length fname val $8;
|
|
92
|
+
drop fname val fid is_empty;
|
|
93
|
+
rc=filename(fname,file_path);
|
|
94
|
+
fid=fopen(fname);
|
|
95
|
+
if fid > 0 then do;
|
|
96
|
+
rc=fread(fid);
|
|
97
|
+
is_empty=fget(fid,val);
|
|
98
|
+
end;
|
|
99
|
+
rc=fclose(fid);
|
|
100
|
+
rc=filename(fname);
|
|
101
|
+
if is_empty ne 0 then delete;
|
|
102
|
+
else file_hash=hashing_file("&method",cats(file_path),0);
|
|
103
|
+
end;
|
|
104
|
+
hash_duration=datetime()-ts;
|
|
105
|
+
run;
|
|
106
|
+
|
|
107
|
+
proc sort data=&outds ;
|
|
108
|
+
by descending level directory file_path;
|
|
109
|
+
run;
|
|
110
|
+
|
|
111
|
+
data _null_;
|
|
112
|
+
set &outds;
|
|
113
|
+
call symputx('maxlevel',level,'l');
|
|
114
|
+
stop;
|
|
115
|
+
run;
|
|
116
|
+
|
|
117
|
+
/* now hash the hashes to populate folder hashes, starting from the bottom */
|
|
118
|
+
%do curlevel=&maxlevel %to 0 %by -1;
|
|
119
|
+
data work._data_ (keep=directory file_hash);
|
|
120
|
+
set &outds;
|
|
121
|
+
where level=&curlevel;
|
|
122
|
+
by descending level directory file_path;
|
|
123
|
+
length str $32767 tmp_hash $32;
|
|
124
|
+
retain str tmp_hash ;
|
|
125
|
+
/* reset vars when starting a new directory */
|
|
126
|
+
if first.directory then do;
|
|
127
|
+
str='';
|
|
128
|
+
tmp_hash='';
|
|
129
|
+
i=0;
|
|
130
|
+
end;
|
|
131
|
+
/* hash each chunk of 100 file paths */
|
|
132
|
+
i+1;
|
|
133
|
+
str=cats(str,file_hash);
|
|
134
|
+
if mod(i,100)=0 or last.directory then do;
|
|
135
|
+
tmp_hash=hashing("&method",cats(tmp_hash,str));
|
|
136
|
+
str='';
|
|
137
|
+
end;
|
|
138
|
+
/* output the hash at directory level */
|
|
139
|
+
if last.directory then do;
|
|
140
|
+
file_hash=tmp_hash;
|
|
141
|
+
output;
|
|
142
|
+
end;
|
|
143
|
+
if last.level then stop;
|
|
144
|
+
run;
|
|
145
|
+
%let tempds=&syslast;
|
|
146
|
+
/* join the hash back into the main table */
|
|
147
|
+
proc sql undo_policy=none;
|
|
148
|
+
create table &outds as
|
|
149
|
+
select a.directory
|
|
150
|
+
,coalesce(b.file_hash,a.file_hash) as file_hash
|
|
151
|
+
,a.hash_duration
|
|
152
|
+
,a.file_path
|
|
153
|
+
,a.file_or_folder
|
|
154
|
+
,a.level
|
|
155
|
+
from &outds a
|
|
156
|
+
left join &tempds b
|
|
157
|
+
on a.file_path=b.directory
|
|
158
|
+
order by level desc, directory, file_path;
|
|
159
|
+
drop table &tempds;
|
|
160
|
+
%end;
|
|
161
|
+
|
|
162
|
+
%mend mp_hashdirectory;
|
package/package.json
CHANGED