This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_anon_queries.sh
executable file
·83 lines (73 loc) · 1.81 KB
/
generate_anon_queries.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
#
# Generate queries to anonymize OpenDataPhilly PostgreSQL DB dumps.
# Run query against local PostgreSQL DB.
# 1st Argument: DB Name
# 2nd Argument: Query
run_query ()
{
docker run -i --rm -e PGPASSWORD=odp --link odp_export:postgres postgres \
psql -h postgres -U postgres -t -A "$1" \
-c "$2";
}
# Insane hack. Establishes named piped connected to interactive Python shell,
# so that we don't need to reimport the Faker packages for every function call.
establish_fake_context ()
{
mkfifo /tmp/faker_in
python -i < /tmp/faker_in >> /tmp/faker_out &
exec 3>/tmp/faker_in
echo "import string; from faker import Faker; fake = Faker();" >&3
}
close_fake_context ()
{
exec 3>&-
rm -f /tmp/faker*
}
# Wait for the Python shell to finish.
wait_for_it ()
{
while ! [ -s /tmp/faker_out ]; do
:
done
}
# Generate fake data.
# 1st Argument: Anything that `$ faker profile` returns.
fake ()
{
cp /dev/null /tmp/faker_out
echo "print(fake.profile()['$1']);" >&3
wait_for_it
echo $(tail -n 1 /tmp/faker_out)
}
# Generate a fake SHA.
fake_sha ()
{
cp /dev/null /tmp/faker_out
echo "print(fake.sha1(raw_output=False));" >&3
wait_for_it
echo $(tail -n 1 /tmp/faker_out)
}
#Generate a fake UUID.
fake_uuid4 ()
{
cp /dev/null /tmp/faker_out
echo "print(fake.uuid4());" >&3
wait_for_it
echo $(tail -n 1 /tmp/faker_out)
}
establish_fake_context
for user_id in $(run_query ckan_default 'select id from "user"'); do
echo "\
UPDATE \"user\" \
SET \"name\" = '$(fake username)${RANDOM}', \
fullname = '$(fake name)', \
email = '${RANDOM}$(fake mail)', \
password = '$(fake_sha)', \
apikey = '$(fake_uuid4)', \
reset_key = null, \
about = null, \
created = '2017-08-10 13:32:27.583108' \
WHERE id = '$user_id';";
done
close_fake_context