Skip to content

Commit 3697180

Browse files
committed
Reverted merge of timezone-aware migration efforts.
- Legacy-Id: 18792
1 parent 0ae0044 commit 3697180

183 files changed

Lines changed: 2934 additions & 3177 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/add-old-drafts-from-archive.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/usr/bin/env python
2+
# Copyright The IETF Trust 2017-2019, All Rights Reserved
3+
4+
import datetime
5+
import os
6+
import sys
7+
from pathlib2 import Path
8+
from contextlib import closing
9+
10+
os.environ["DJANGO_SETTINGS_MODULE"] = "ietf.settings"
11+
12+
import django
13+
django.setup()
14+
15+
from django.conf import settings
16+
from django.core.validators import validate_email, ValidationError
17+
from ietf.utils.draft import Draft
18+
from ietf.submit.utils import update_authors
19+
20+
import debug # pyflakes:ignore
21+
22+
from ietf.doc.models import Document, NewRevisionDocEvent, DocEvent, State
23+
from ietf.person.models import Person
24+
25+
system = Person.objects.get(name="(System)")
26+
expired = State.objects.get(type='draft',slug='expired')
27+
28+
names = set()
29+
print 'collecting draft names ...'
30+
versions = 0
31+
for p in Path(settings.INTERNET_DRAFT_PATH).glob('draft*.txt'):
32+
n = str(p).split('/')[-1].split('-')
33+
if n[-1][:2].isdigit():
34+
name = '-'.join(n[:-1])
35+
if '--' in name or '.txt' in name or '[' in name or '=' in name or '&' in name:
36+
continue
37+
if name.startswith('draft-draft-'):
38+
continue
39+
if name == 'draft-ietf-trade-iotp-v1_0-dsig':
40+
continue
41+
if len(n[-1]) != 6:
42+
continue
43+
if name.startswith('draft-mlee-'):
44+
continue
45+
names.add('-'.join(n[:-1]))
46+
47+
count=0
48+
print 'iterating through names ...'
49+
for name in sorted(names):
50+
if not Document.objects.filter(name=name).exists():
51+
paths = list(Path(settings.INTERNET_DRAFT_PATH).glob('%s-??.txt'%name))
52+
paths.sort()
53+
doc = None
54+
for p in paths:
55+
n = str(p).split('/')[-1].split('-')
56+
rev = n[-1][:2]
57+
with open(str(p)) as txt_file:
58+
raw = txt_file.read()
59+
try:
60+
text = raw.decode('utf8')
61+
except UnicodeDecodeError:
62+
text = raw.decode('latin1')
63+
try:
64+
draft = Draft(text, txt_file.name, name_from_source=True)
65+
except Exception as e:
66+
print name, rev, "Can't parse", p,":",e
67+
continue
68+
if draft.errors and draft.errors.keys()!=['draftname',]:
69+
print "Errors - could not process", name, rev, datetime.datetime.fromtimestamp(p.stat().st_mtime), draft.errors, draft.get_title().encode('utf8')
70+
else:
71+
time = datetime.datetime.fromtimestamp(p.stat().st_mtime)
72+
if not doc:
73+
doc = Document.objects.create(name=name,
74+
time=time,
75+
type_id='draft',
76+
title=draft.get_title(),
77+
abstract=draft.get_abstract(),
78+
rev = rev,
79+
pages=draft.get_pagecount(),
80+
words=draft.get_wordcount(),
81+
expires=time+datetime.timedelta(settings.INTERNET_DRAFT_DAYS_TO_EXPIRE),
82+
)
83+
DocAlias.objects.create(name=doc.name).docs.add(doc)
84+
doc.states.add(expired)
85+
# update authors
86+
authors = []
87+
for author in draft.get_author_list():
88+
full_name, first_name, middle_initial, last_name, name_suffix, email, country, company = author
89+
90+
author_name = full_name.replace("\n", "").replace("\r", "").replace("<", "").replace(">", "").strip()
91+
92+
if email:
93+
try:
94+
validate_email(email)
95+
except ValidationError:
96+
email = ""
97+
98+
def turn_into_unicode(s):
99+
if s is None:
100+
return u""
101+
102+
if isinstance(s, unicode):
103+
return s
104+
else:
105+
try:
106+
return s.decode("utf-8")
107+
except UnicodeDecodeError:
108+
try:
109+
return s.decode("latin-1")
110+
except UnicodeDecodeError:
111+
return ""
112+
113+
author_name = turn_into_unicode(author_name)
114+
email = turn_into_unicode(email)
115+
company = turn_into_unicode(company)
116+
117+
authors.append({
118+
"name": author_name,
119+
"email": email,
120+
"affiliation": company,
121+
"country": country
122+
})
123+
dummysubmission=type('', (), {})() #https://stackoverflow.com/questions/19476816/creating-an-empty-object-in-python
124+
dummysubmission.authors = authors
125+
update_authors(doc,dummysubmission)
126+
127+
# add a docevent with words explaining where this came from
128+
events = []
129+
e = NewRevisionDocEvent.objects.create(
130+
type="new_revision",
131+
doc=doc,
132+
rev=rev,
133+
by=system,
134+
desc="New version available: <b>%s-%s.txt</b>" % (doc.name, doc.rev),
135+
time=time,
136+
)
137+
events.append(e)
138+
e = DocEvent.objects.create(
139+
type="comment",
140+
doc = doc,
141+
rev = rev,
142+
by = system,
143+
desc = "Revision added from id-archive on %s by %s"%(datetime.date.today(),sys.argv[0]),
144+
time=time,
145+
)
146+
events.append(e)
147+
doc.time = time
148+
doc.rev = rev
149+
doc.save_with_history(events)
150+
print "Added",name, rev

0 commit comments

Comments
 (0)