From ef26f79b29eaab1dd19d70d41c3b34fc40ca876a Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 14:16:30 +0800 Subject: [PATCH 01/48] Update 20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md --- ...cript Parser that Creates a Language in 200 Lines of Code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md b/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md index df2178ce8e..087a8d3fda 100644 --- a/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md +++ b/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md @@ -2,7 +2,7 @@ Translating by ucasFL Ohm: JavaScript Parser that Creates a Language in 200 Lines of Code =========== - +Ohm: 一种能用可以用两百行代码创造一种语言的 JavaScript 解释器 Parsers are an incredibly useful software libraries. While conceptually simple, they can be challenging to implement and are often considered a dark art in computer science. In this blog series, I’ll show you why you don’t need to be Harry Potter to master parsers. But bring your wand just in case. We’ll explore a new open source Javascript library called Ohm that makes building parsers easy and easier to reuse. In this series, we use Ohm to recognize numbers, build a calculator, and more. By the end of this series you will have created a complete programming language in under 200 lines of code. This powerful tool will let you do things that you might have thought impossible otherwise. From ced4235a5bfc61106c211d2de5ab42858150dad2 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 14:23:31 +0800 Subject: [PATCH 02/48] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...个致力于“计算”,大学,女权,科技多样性的人 | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 diff --git a/translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 b/translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 new file mode 100644 index 0000000000..daf386bb36 --- /dev/null +++ b/translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 @@ -0,0 +1,49 @@ +![Image by : Photo by Becky Svartström. Modified by Opensource.com. CC BY-SA 4.0](http://opensource.com/sites/default/files/styles/image-full-size/public/images/life/osdc-lead-patriciatorvalds.png) +Image by : Photo by Becky S. Modified by Opensource.com. [CC BY-SA 4.0][1] +图片来源:照片来自Becky Svartström, 修改自Opensource.com. +Patricia Torvalds 并不是那个在 Linux 和开源领域非常有名同样叫做 Torvalds 的人。 +![](http://opensource.com/sites/default/files/images/life-uploads/ptorvalds.png) +18岁的时候,Patricia 已经是一个有多项科技成就并且拥有开源产业经验的女权主义者。紧接着,她把目光投入到在杜克大学地球科学学院工程学第一学年的学习中,以实习生的身份在位于美国奥勒冈州伯特兰市的 Puppet 实验室工作。但不久后,她又直接去了北卡罗纳州的达拉莫,开始秋季学期的大学学习。 +在这次独家采访中,Patricia 表示,使她对计算机科学和工程学感兴趣的(剧透警告:不是她的父亲)原因,包括高中时候对科技的偏爱,女权主义在她的生活中扮演了重要角色以及对科技多样性缺乏的思考。 +![](http://opensource.com/sites/default/files/images/life/Interview%20banner%20Q%26A.png) +###什么东西使你钟情于学习计算机科学和工程学?### +我在科技方面的兴趣的确产生于高中时代。我曾一度想投身于生物学,直到大约大学二年级的时候。大二结束以后,我在波特兰 VA 当网页设计实习生。与此同时,我参加了一个叫做“风险勘探”的工程学课程,在我大二学年的后期,我们把一个水下机器人送到了太平洋。但是,转折点大概是在青年时代的中期当我因被授予“NCWIT Aspiration in Computing"奖而被称作地区和民族英雄的时候出现的。 +这个奖项的获得我感觉确立了自己的兴趣。当然,我认为最重要的部分是我加入到一个所有获奖者在里面的 Facebook 群,里面那些已经获奖的女孩们简直难以置信的相互支持。由于在 XV 和 VA +的工作,我在获奖前就已经确定了致力于计算机科学,但是和这些女孩的交谈更加坚定了这份兴趣,使之更加强壮。再后来,初高年级后期的时候教授 XV 也使我体会到工程学和计算机科学对我来说的确很有趣。 +###你打算学习什么?毕业以后你已经知道自己想干什么了吗?### +我希望主修力学或电气科学以及计算机工程学和计算机科学还有女性学。毕业以后,我希望在一个支持或者创造科技为社会造福的公司工作,或者自己开公司。 +###我的女儿在高中有一门 Visual Basic的编程课。她是整个班上唯一的一个女生,并且以疲倦和痛苦的经历结束这门课程。你的经历是什么样的呢?### +我的高中在高年级的时候开设计算机科学的课程,我也学习了 Visual Basic!这门课不是很糟糕,但我的确是20多个人的班级里唯一的三四个女生之一。其他的计算机课程似乎也有相似的性别比例差异。然而,我所在的高中极其小并且老师对科技非常支持和包容,所以我并没有感到厌倦。希望在未来的一些年里计算机方面的课程会变得更加多样化。 +###你的学校做了哪些促进科技的智举?它们如何能够变得更好?### +我的高中学校给了我们长时间的机会接触到计算机,老师们会突然在不相关的课程上安排科技基础任务,比如有好多次任务,我们必须建一个供社会学习课程使用的网站,我认为这很棒因为它使我们每一个人都能接触到科技。机器人俱乐部也很活跃并且资金充足,但是非常小,我不是其中的成员。学校的科技/工程学项目中一个非常强大的组成部分是一门叫做”风险勘测“的学生教学工程学课程,这是一门需要亲自动手的课程,并且每年处理一个工程学或者计算机科学难题。我和我的一个同学在这儿教授了两年,在课程结束以后,有学生上来告诉我他们对从事工程学或者计算机科学感兴趣。 +然而,我的高中没有特别的关注于让年轻女性加入到这些项目中来,并且在人种上也没有呈现多样化。计算机课程和俱乐部大量的主要成员都是男性白人。这的确应该能够有所改善。 +###在成长过程中,你如何在家使用科技?### +老实说,小的时候,我使用电脑(我的父亲设了一个跟踪装置,当我们上网一个小时就会断线)玩尼奥宠物和或者相似的游戏。我想我本可以毁坏跟踪装置或者在不连接网络的情况下玩游戏,但我没有这样做。我有时候也会和我的父亲做一些小的科学项目,我还记得我和他在电脑终端上打印出”Hello world"无数次。但是大多数时候,我都是和我的妹妹一起玩网络游戏,直到高中的时候才开始学习“计算”。 +###你在高中学校的女权俱乐部很积极,从这份经历中你学到了什么?现在对你来说什么女权问题是最重要的?### +在高中二年级的后期,我和我的朋友一起建立了女权俱乐部。刚开始,我们受到了很多反对和抵抗,并且这从来就没有完全消失过。到我们毕业的时候,女权主义理想已经彻底成为了学校文化的一个部分。我们在学校做的女权主义工作通常是以一些比较直接的方式并集中于像着装要求这样一些问题。 +就我个人来说,我更集中于交叉地带的女权主义,把女权主义运用到缓解其他方面的压迫比如种族歧视和阶级歧视。Facebook 网页《Gurrilla Feminism》是交叉地带女权主义一个非常好的例子并且我从中学到了很多。我目前管理波特兰分支。 +在科技多样性方面女权主义对我也非常重要,尽管作为一名和科技世界有很强联系的上流社会女性,女权主义问题对我产生的影响相比其他人来说非常少,我所涉及的交叉地带女权主义也是同样的。出版集团比如《Model View Culture》非常鼓舞我,并且我很感激 Shanley Kane 所做的一切。 +###你会给想教他们的孩子学习编程的父母什么样的建议?### +老实说,从没有人把我推向计算机科学或者工程学。正如我前面说的,在很长一段时间里,我想成为一名遗传学家。大二结束的那个夏天,我在 VA 当了一个夏季的网页设计实习生,这彻底改变了我之前的想法。所以我不知道我是否能够完整的回答这个问题。 +我的确认为真实的兴趣很重要。如果在我12岁的时候,我的父亲让我坐在一台电脑前,教我安装网站服务器,我认为我不会对计算机科学感兴趣。相反,我的父母给了我很多可以支配的自由让我去做自己想做的事情,绝大多数时候是为我的尼奥宠物游戏编糟糕的HTML网站。比我小的妹妹们没有一个对工程学或计算机科学感兴趣,我的父母也不在乎。我感到很幸运的是我的父母给了我和我的妹妹们鼓励和资源去探索自己的兴趣。 +仍然,在我成长过程中我也常说未来要和我的父亲做同样的职业,尽管我还不知道我父亲是干什么的,只知道他有一个很酷的工作。另外,中学的时候有一次,我告诉我的父亲这件事,然后他没有发表什么看法只是告诉我高中的时候不要想这事。所以我猜想这从一定程度上刺激了我。 +###对于开源社区的领导者们,你有什么建议给他们来吸引和维持更加多样化的贡献者?### +我实际上在开源社区不是特别积极和活跃。和女性讨论“计算”我感觉更舒服。我是“NCWIT Aspirarion in Computing"网站的一名成员,这是我对科技有持久兴趣的一个重要方面,同样也包括Facebook群”Ladies Storm Hackathons". +我认为对于吸引和维持多种多样有天赋的贡献者,安全空间很重要。我过去看到在一些开源社区有人发表关于女性歧视和种族主义的评论,人们指出这一问题随后该人被解职。我认为要维持一个专业的社区必须就骚扰事件和不正当行为有一个很高的标准。当然,人们已经有或者将有很多的选择关于在开源社区或其他任何社区能够表达什么。然而,如果社区领导人真的想吸引和维持多种多样有天赋的人员,他们必须创造一个安全的空间并且把社区成员维持在很高的标准上。我也认为一些一些社区领导者不明白多样性的价值。很容易说明科技象征着精英社会,并且很多人被科技忽视的原因是他们不感兴趣,这一问题在很早的准备过程中就提出了。他们争论如果一个人在自己的工作上做得很好,那么他的性别或者民族还有性取向这些情况都变得不重要了。这很容易反驳,但我不想为错误找理由。我认为多样性缺失是一个错误,我们应该为之负责并尽力去改善这件事。 +-------------------------------------------------------------------------------- + +via: http://opensource.com/life/15/8/patricia-torvalds-interview + +作者:[Rikki Endsley][a] +译者:[ucasFL](https://github.com/ucasFL) +校对:[校对者ID](https://github.com/校对者ID) +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:http://opensource.com/users/rikki-endsley +[1]:https://creativecommons.org/licenses/by-sa/4.0/ +[2]:https://puppetlabs.com/ +[3]:https://www.aspirations.org/ +[4]:https://www.facebook.com/guerrillafeminism +[5]:https://modelviewculture.com/ +[6]:https://www.aspirations.org/ +[7]:https://www.facebook.com/groups/LadiesStormHackathons/ From 9b3be3454f83143c6b02f65bc2dcbcdf7cfc07c4 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 15:45:39 +0800 Subject: [PATCH 03/48] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=88=E6=A0=87=E9=A2=98=E4=BF=AE=E6=94=B9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...人 => Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename translated/talk/{Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 => Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化} (91%) diff --git a/translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 b/translated/talk/Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 similarity index 91% rename from translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 rename to translated/talk/Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 index daf386bb36..5290015524 100644 --- a/translated/talk/Patricia Torvalds: 一个致力于“计算”,大学,女权,科技多样性的人 +++ b/translated/talk/Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 @@ -4,7 +4,7 @@ Image by : Photo by Becky S. Modified by Opensource.com. [CC BY-SA 4.0][1] Patricia Torvalds 并不是那个在 Linux 和开源领域非常有名同样叫做 Torvalds 的人。 ![](http://opensource.com/sites/default/files/images/life-uploads/ptorvalds.png) 18岁的时候,Patricia 已经是一个有多项科技成就并且拥有开源产业经验的女权主义者。紧接着,她把目光投入到在杜克大学地球科学学院工程学第一学年的学习中,以实习生的身份在位于美国奥勒冈州伯特兰市的 Puppet 实验室工作。但不久后,她又直接去了北卡罗纳州的达拉莫,开始秋季学期的大学学习。 -在这次独家采访中,Patricia 表示,使她对计算机科学和工程学感兴趣的(剧透警告:不是她的父亲)原因,包括高中时候对科技的偏爱,女权主义在她的生活中扮演了重要角色以及对科技多样性缺乏的思考。 +在这次独家采访中,Patricia 表示,使她对计算机科学和工程学感兴趣的(剧透警告:不是她的父亲)原因,包括高中时候对科技的偏爱,女权主义在她的生活中扮演了重要角色以及对科技多元化缺乏的思考。 ![](http://opensource.com/sites/default/files/images/life/Interview%20banner%20Q%26A.png) ###什么东西使你钟情于学习计算机科学和工程学?### 我在科技方面的兴趣的确产生于高中时代。我曾一度想投身于生物学,直到大约大学二年级的时候。大二结束以后,我在波特兰 VA 当网页设计实习生。与此同时,我参加了一个叫做“风险勘探”的工程学课程,在我大二学年的后期,我们把一个水下机器人送到了太平洋。但是,转折点大概是在青年时代的中期当我因被授予“NCWIT Aspiration in Computing"奖而被称作地区和民族英雄的时候出现的。 @@ -27,9 +27,9 @@ Patricia Torvalds 并不是那个在 Linux 和开源领域非常有名同样叫 老实说,从没有人把我推向计算机科学或者工程学。正如我前面说的,在很长一段时间里,我想成为一名遗传学家。大二结束的那个夏天,我在 VA 当了一个夏季的网页设计实习生,这彻底改变了我之前的想法。所以我不知道我是否能够完整的回答这个问题。 我的确认为真实的兴趣很重要。如果在我12岁的时候,我的父亲让我坐在一台电脑前,教我安装网站服务器,我认为我不会对计算机科学感兴趣。相反,我的父母给了我很多可以支配的自由让我去做自己想做的事情,绝大多数时候是为我的尼奥宠物游戏编糟糕的HTML网站。比我小的妹妹们没有一个对工程学或计算机科学感兴趣,我的父母也不在乎。我感到很幸运的是我的父母给了我和我的妹妹们鼓励和资源去探索自己的兴趣。 仍然,在我成长过程中我也常说未来要和我的父亲做同样的职业,尽管我还不知道我父亲是干什么的,只知道他有一个很酷的工作。另外,中学的时候有一次,我告诉我的父亲这件事,然后他没有发表什么看法只是告诉我高中的时候不要想这事。所以我猜想这从一定程度上刺激了我。 -###对于开源社区的领导者们,你有什么建议给他们来吸引和维持更加多样化的贡献者?### +###对于开源社区的领导者们,你有什么建议给他们来吸引和维持更加多元化的贡献者?### 我实际上在开源社区不是特别积极和活跃。和女性讨论“计算”我感觉更舒服。我是“NCWIT Aspirarion in Computing"网站的一名成员,这是我对科技有持久兴趣的一个重要方面,同样也包括Facebook群”Ladies Storm Hackathons". -我认为对于吸引和维持多种多样有天赋的贡献者,安全空间很重要。我过去看到在一些开源社区有人发表关于女性歧视和种族主义的评论,人们指出这一问题随后该人被解职。我认为要维持一个专业的社区必须就骚扰事件和不正当行为有一个很高的标准。当然,人们已经有或者将有很多的选择关于在开源社区或其他任何社区能够表达什么。然而,如果社区领导人真的想吸引和维持多种多样有天赋的人员,他们必须创造一个安全的空间并且把社区成员维持在很高的标准上。我也认为一些一些社区领导者不明白多样性的价值。很容易说明科技象征着精英社会,并且很多人被科技忽视的原因是他们不感兴趣,这一问题在很早的准备过程中就提出了。他们争论如果一个人在自己的工作上做得很好,那么他的性别或者民族还有性取向这些情况都变得不重要了。这很容易反驳,但我不想为错误找理由。我认为多样性缺失是一个错误,我们应该为之负责并尽力去改善这件事。 +我认为对于吸引和维持多种多样有天赋的贡献者,安全空间很重要。我过去看到在一些开源社区有人发表关于女性歧视和种族主义的评论,人们指出这一问题随后该人被解职。我认为要维持一个专业的社区必须就骚扰事件和不正当行为有一个很高的标准。当然,人们已经有或者将有很多的选择关于在开源社区或其他任何社区能够表达什么。然而,如果社区领导人真的想吸引和维持多元化有天赋的人员,他们必须创造一个安全的空间并且把社区成员维持在很高的标准上。我也认为一些一些社区领导者不明白多元化的价值。很容易说明科技象征着精英社会,并且很多人被科技忽视的原因是他们不感兴趣,这一问题在很早的准备过程中就提出了。他们争论如果一个人在自己的工作上做得很好,那么他的性别或者民族还有性取向这些情况都变得不重要了。这很容易反驳,但我不想为错误找理由。我认为多元化缺失是一个错误,我们应该为之负责并尽力去改善这件事。 -------------------------------------------------------------------------------- via: http://opensource.com/life/15/8/patricia-torvalds-interview From b142c98b7ea6bf84bb526d0e218da226855c33c6 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 16:11:52 +0800 Subject: [PATCH 04/48] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=88=E6=96=87=E4=BB=B6=E5=90=8D=E4=BF=AE=E6=94=B9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... Patricia Torvalds on computing, college, feminism and increasing diversity in tech} | 1 + 1 file changed, 1 insertion(+) rename translated/talk/{Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 => Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech} (99%) diff --git a/translated/talk/Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 b/translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech similarity index 99% rename from translated/talk/Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 rename to translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech index 5290015524..e650dfde93 100644 --- a/translated/talk/Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义与科技界的多元化 +++ b/translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech @@ -1,3 +1,4 @@ +Torvalds2.0: Patricia Torvalds 谈“计算”,大学,女权主义和科技界的多元化 ![Image by : Photo by Becky Svartström. Modified by Opensource.com. CC BY-SA 4.0](http://opensource.com/sites/default/files/styles/image-full-size/public/images/life/osdc-lead-patriciatorvalds.png) Image by : Photo by Becky S. Modified by Opensource.com. [CC BY-SA 4.0][1] 图片来源:照片来自Becky Svartström, 修改自Opensource.com. From 668df8878b3a9b13709e44c7d14b39337e6590f1 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 22:49:25 +0800 Subject: [PATCH 05/48] Update 20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md --- ...Creates a Language in 200 Lines of Code.md | 189 ++++++------------ 1 file changed, 60 insertions(+), 129 deletions(-) diff --git a/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md b/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md index 087a8d3fda..810ba28c2a 100644 --- a/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md +++ b/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md @@ -1,64 +1,41 @@ Translating by ucasFL -Ohm: JavaScript Parser that Creates a Language in 200 Lines of Code + =========== -Ohm: 一种能用可以用两百行代码创造一种语言的 JavaScript 解释器 -Parsers are an incredibly useful software libraries. While conceptually simple, they can be challenging to implement and are often considered a dark art in computer science. In this blog series, I’ll show you why you don’t need to be Harry Potter to master parsers. But bring your wand just in case. - -We’ll explore a new open source Javascript library called Ohm that makes building parsers easy and easier to reuse. In this series, we use Ohm to recognize numbers, build a calculator, and more. By the end of this series you will have created a complete programming language in under 200 lines of code. This powerful tool will let you do things that you might have thought impossible otherwise. - -### Why Parsers are Hard - -Parsers are useful. There are lots of times you might need a parser. A new file format might come along that you need to process and no one else has written a library for it yet. Or maybe you find files in an old file format and the existing parsers aren’t built for the platform you need. I’ve seen this happen over and over. Code will come and go but data is forever. - -Fundamentally parsers are simple: just transform one data structure into another. So why does it feel like you need to be Dumbledore to figure them out? - -The challenge that parsers have historically been surprisingly difficult to write, and most of the existing tools are old and assume a fair amount of arcane computer science knowledge. If you took a compilers class in college the textbook may well have techniques from the 1970s. Fortunately, parser technology has improved a great deal since then. - -Typically a parser is created by defining what you want to parse using a special syntax called a formal grammar. Then you feed this into several tools like Bison and Yacc which generate a bunch of C code that you the need to modify or link into whatever programming language you are actually writing in. The other option is to manually write a parser in your preferred language, which is slow and error prone. That’s a lot of extra work before you get to actually use the parser. - -Imagine if your description of the thing you wanted to parse, the grammar, was also the parser? What if you could just run the grammar directly, then add hooks only where you want? That’s what Ohm does. - -### Introducing Ohm - -[Ohm][1] is a new kind of parsing system. While it resembles the grammars you may have seen in text books it’s a lot more powerful and easier to use. With Ohm you write your format definition in a very flexible syntax in a .ohm file, then attach semantic meaning to it using your host language. For this blog we will use JavaScript as the host language. - -Ohm is based on years of research into making parsers easier and more flexible. VPRI’s [STEPS program][2] (pdf) created many custom languages for specific tasks (like a fully parallelizable graphics renderer in 400 lines of code!) using Ohm’s precursor [OMeta][3]. - -Ohm has many interesting features and notations, but rather than explain them all I think we should just dive in and build something. - -### Parsing Integers - -Let’s parse some numbers. It seems like this would be easy. Just look for adjacent digits in a string of text; but let’s try to handle all forms of numbers: Integers and floating point. Hex and octal. Scientific notation. A leading negative digit. Parsing numbers is easy. Doing it right is hard - -To build this code by hand would be difficult and buggy, with lots of special cases which sometimes conflict with each other. A regular expression could probably do it, but would be ugly and hard to maintain. Let’s do it with Ohm instead. - -Every parser in Ohm involves three parts: the grammar, the semantics, and the tests. I usually pick part of the problem and write tests for it, then build enough of the grammar and semantics to make the tests pass. Then I pick another part of the problem, add more tests, update the grammar and semantics, while making sure all of the tests continue to pass. Even with our new powerful tool, writing parsers is still conceptually complicated. Tests are the only way to build parsers in a reasonable manner. Now let’s dig in. - -We’ll start with an integer number. An integer is composed of a sequences of digits next to each other. Let’s put this into a file called grammar.ohm: - +Ohm: 一种可以用两百行代码创造一种语言的 JavaScript 解释器 +解释器是一种非常有用的软件库。从概念上简单的说,它们的实现很有挑战并且在计算机科学中经常被认为是暗黑艺术。在这个系列的博文中,我会向你们展示为什么你不需要成为哈利波特就能够很好的控制解释器。但是为了以防万一带上你的魔杖。 +我们将探索一种叫做 Ohm 的新的开源库,它使得搭建解释器很简单并且更加容易再利用。在这个系列里,我们使用 Ohm 去识别数字,构建计算器等等。在这个系列的最后你将已经用少于 200 行的代码发明了一种完整的编程语言。这个强大的工具将让你能够做一些你可能过去认为不可能的事情。 +###为什么解释器很困难? +解释器非常有用。在很多时候你可能需要一个解释器。一种新的文件格式可能出现,你需要去处理但还没有人为它写了一个库;或者你发现了一种老格式的文件但是现存的解释器不能构建你需要的平台。我已经看到这样的事发生无数次。代码会来来去去但数据却是永恒的。 +基础的解释器很简单:只是把一个数据结构转化成另一个。所以为什么感觉你需要成为 邓布利多【魔法师】才能够把它们做出来。 +解释器的一些历史性的挑战是很难写,绝大多数工具很老并且假设了大量晦涩难懂的计算机科学知识。如果你在大学里上过编译器课程那么课本里可能也有从 1970 年以来的技术。幸运的是,解释器技术从那时候起已经提高了很多。 +代表性地,解释器是通过使用一种叫作形式语法的特殊语法来定义你想要解析的东西这样发明的,然后你需要把它放入像 Bison 和 Yacc 的工具中,这些工具能够产生一堆你需要修改的 C 代码或者链接到你实际写入额的编程语言。另外的选择是用你更喜欢的语言亲自动手写一个解释器,这很慢且很容易出错,在你能够真正使用它之前还有许多额外的工作。 +想像一下,是否你关于你想要解析的东西的语法描述也是解释器?如果你能够仅仅直接运行语法,然后在你需要的地方增加挂钩,那是什么?那就是 Ohm 所做的事。 +###解释器简介 +[Ohm][1]是一种新的解析系统。它类似你可能已经在课本里面看到的语法并且它更强大,使用起来更简单。通过 Ohm, 你能够使用一种灵活的语法以 .ohm 文件格式来写格式定义,然后使用你的宿主语言把语义加入到里面。在这篇博文里,我们将用 JavaScript 作为宿主语言。 +Ohm 建立在一个为制造更简单、更灵活的解释器的一个多年调查基础之上。VPRI 的 [STEPS program](pdf) 使用 Ohm 的前驱为许多特殊的工作创造了专门的语言(比如一个有 400 行代码的平行制图描绘器)[Ometa][3]. +Ohm 有许多有趣的特点和符号,但是不是要全部解释它们,我认为我们应该只需投入其中并构建一些东西。 +###解析整数 +让我们来解析一些数字。这看起来会很简单,只需在一个文本串中寻找毗邻的数字,但是让我们尝试去处理所有形式的数字:整数和浮点数,十六进制数和八进制数,科学计数,负数。解析数字很简单,正确解析却很难。 +亲自构建这个代码将会很困难,会有很多故障,会伴随有许多特殊的情况,比如有时会相互矛盾。 +用 Ohm 构建的解释器涉及三个部分:语法、语义和测试。我通常挑选一个问题的一部分为它写测试,然后构建足够的语法和语义来使测试通过。然后我再挑选问题的另一部分,增加更多的测试,更新语法和语义,从而确保所有的测试能够持续通过。即使我们有了新的强大的工具,写解释器从概念上来说依旧很困难。测试是用一种合理的方式来构建解释器的唯一方法。现在,让我们开始工作。 +我们将从整数开始。一个整数由一系列相互毗邻的数字组成。让我们把下面的内容放入一个叫做 grammar.ohm 的文件中: ``` CoolNums { // just a basic integer Number = digit+ } ``` - -This creates a single rule called Number which matches one or more digits. The + means one or more, just like in a regular expression. This rule will match if there is one digit or more than one digit. It won’t match if there are zero digits or something something other than a digit. A digit is defined as the characters for the numbers 0 to 9. digit is also a rule like Number is, but it’s one of Ohm’s built in rules so we don’t have to define it ourselves. We could override if it we wanted to but that wouldn’t make sense in this case. After all we don’t plan to invent a new form of number (yet!) - -Now we can read in this grammar and process it with the Ohm library. - -Put this into test1.js - +这创造了一条撮合一个或多个数字叫作 Number 的单一规则。+ 意味着一个或更多,就像一个常规的表达。当有一个或更多的数字时,这个规则将会撮合它们,如果没有数字或者有一些不是数字的东西将不会撮合。一个数字定义成从 0 到 9 其中的一个字符。数字也是像 Number 一样的规则,但是它是 Ohm 的其中一条构建规则因此我们不需要去定义它。我们可以推翻它如果我们想的话但在这时候这没有任何意义,毕竟我们不打算去发明一种新的数。 +现在,我们可以读入这个语法并用 Ohm 库来运行它。 +把它放入 test1.js ``` var ohm = require('ohm-js'); var fs = require('fs'); var assert = require('assert'); var grammar = ohm.grammar(fs.readFileSync('src/blog_numbers/syntax1.ohm').toString()); ``` - -The ohm.grammar call will read in the file and parse it into a grammar object. Now we can add semantics. Add this to your Javascript file: - +Ohm 的语法调用将把文件读入并解释成一个语法对象。现在我们可以增加一些语义。把下面内容增加到你的 JavaScript 文件中: ``` var sem = grammar.createSemantics().addOperation('toJS', { Number: function(a) { @@ -66,16 +43,11 @@ var sem = grammar.createSemantics().addOperation('toJS', { } }); ``` - -This creates a set of semantics called sem with the operation toJS. The semantics are essentially a bunch of functions matching up to each rule in the grammar. Each function will be called when the corresponding rule in the grammar is parsed. The Number function above will be called when the Number rule in the grammar is parsed. The grammar defines what chunks are in the language. The semantics define what to do with chunks once they’ve been parsed. - -Our semantics functions can do anything we want, such as print debugging information, create objects, or recursively call toJS on any sub-nodes. In this case we just want to convert the matched text into a real Javascript integer. - -All semantic functions have an implicit this object with some useful properties. The source property represents the part of the input text that matches this node. this.sourceString is the matched input as a string. Calling the built in JavaScript function parseInt turns this string to a number. The 10 argument to parseInt tells JavaScript that we are giving it a number in base ten. If we leave it out then JS will assume it’s base 10 anyway, but I’ve included it because later on we will support base 16 (hex) numbers, so it’s good to be explicit. - -Now that we have some semantics, let’s actually parse something to see if our parser works. How do we know our parser works? By testing it. Lots and lots of testing. Every possible edge case needs a test. - -With the standard assert API, here is a test function which matches some input then applies our semantics to it to turn it into a number, then compares the number with the expected input. +这创造了一系列叫作 sem with the operation to JS[伴有 JavaScript 操作的语义] 的语义。这些语义至关重要,一群函数和语法中的每一条规则相匹配。一个函数将会被调用当与它相匹配的语法规则被解析时。上面的 Number 函数将会被调用当语法中的 Number 规则被解析时。语法定义在语言中 chunks[大块] 是什么,语义定义当 chunks[大块] 被解析时应该做什么。 +语义函数能够做我们想做的任何事,比如打印初故障信息,创造对象,或者递归调用 toJS 作用于任何子节点。此时我们仅仅想把文本转换成真正的 JavaScript 整数。 +所有的语义函数有一个包含一些有用性质的暗含对象。源属性代表输入文本和这个节点相匹配。这个 sourceString[源串] 是一个匹配输入串,调用构建在 JavaScript 中的parseInt 函数会把这个串转换成一个数。parseInt 中 10 这个参数告诉 JavaScript 我们输入的是一个以 10 为基底的数。如果少了这个参数, JavaScript 也会假定以 10 为基底,但是我们把它包含在里面因为后面我们将支持以 16 为基底的数,所以使之明确比较好。 +既然我们有一些语法,让我们来实际解析一些东西看一看我们的解释器是否能够工作。如果知道我们的解释器工作?通过测试它,许多许多的测试,每一个边缘情况都需要一个测试。 +伴随标准断言 API,有一个测试函数能够匹配一些输入并运用我们的语义把它转换成一个数,然后比较转换生成的数和我们期望的输入。 ``` function test(input, answer) { var match = grammar.match(input); @@ -85,19 +57,14 @@ With the standard assert API, here is a test function which matches some input t console.log('success = ', result, answer); } ``` - -That’s it. Now we can write a bunch of tests for different numbers. If the match fails then our script will throw an exception. If not it will print success. Let’s try it out. Add this to the script - +这个函数就是上面这个。现在我们能够为不同的数写一堆测试。如果匹配失败我们的脚本将会丢弃一个例外。如果不能打印成功,让我们尝试一下,把下面这些内容加入到脚本中: ``` test("123",123); test("999",999); test("abc",999); ``` - -Then run the script with node test1.js - -Your output should look like this: - +然后用节点 test.js 运行脚本 +你的输出应该是这样: ``` success = 123 123 success = 999 999 @@ -106,13 +73,9 @@ input failed to match abcLine 1, col 1: ^ Expected a digit ``` - -Cool. The first two succeed and the third one fails, as it should. Even better, Ohm automatically gave us a nice error message pointing to the match failure. - -### Floating Point - -Our parser works, but it doesn’t do anything very interesting. Let’s extend it to parse both integers and floating point numbers. Change the grammar.ohm file to look like this: - +真酷。正如理所当然的那样,前两个成功了,第三个失败了。更好的是,Ohm 自动给了我们一个很棒的错误信息指出匹配失败。 +###浮点数 +我们的解释器工作了,但是它不能做任何非常有趣的事。让我们把它扩展成既能解析整数又能解析浮点数。改变 grammar.ohm 文件使它看起来像下面这样: ``` CoolNums { // just a basic integer @@ -121,11 +84,8 @@ CoolNums { float = digit+ "." digit+ } ``` - -This changes the Number rule to point to either a float or an int. The | means or. We read this as “a Number is composed of a float or an int.” Then int is defined as digit+ and float is defined as digit+ followed by a period followed by another digit+. This means there must be at least one digit before the period and at least one after. If there is not a period then it won’t be a float at all, so int will match instead. - -Now let’s go look at our semantic actions again. Since we now have new rules we need new action functions: one for int and one for float. - +这把 Number 规则改变成指向一个浮点数或者一个整数。我的意思是,我们把这读成"一个 Number 由一个浮点数或者一个整数构成。”然后整数定义成 digit+, 浮点数定义成 digit+ 后面跟着一个句号然后再跟着另一个 digit+. 这意味着在句号前和句号后都至少要有一个数字。如果一个数中没有一个句号那么它就不是一个浮点数,因此就是一个整数。 +现在,让我们再次看一下我们的语义作用。由于我们现在有了新的规则所以我们需要新的作用函数:一个作为整数的,一个作为浮点数的。 ``` var sem = grammar.createSemantics().addOperation('toJS', { Number: function(a) { @@ -141,19 +101,13 @@ var sem = grammar.createSemantics().addOperation('toJS', { } }); ``` - -There’s two things to note here. First, int and float and Number all have matching grammar rules and functions. However, the action for Number no longer does anything interesting. It receives the child node ‘a’ and returns the result of toJS on the child. In other words the Number rule simply returns whatever its child rule matched. Since this is the default behavior of any rule in Ohm we can actually just leave the Number action out. Ohm will do it for us. - -Second, int has one argument a while float has three: a, b, and c. This is because of the rule’s arity. Arity means how many arguments a rule has. If we look back at the grammar, the rule for float is - +这里有两件事情需要注意。首先,整数,浮点数和数都有相匹配的语法规则和函数。然而,针对数的作用不再有任何意义。它接收子节点 'a' 然后通过子节点返回 toJS 的结果。换句话说,Number 规则简单的返回相匹配的子规则。由于这是在 Ohm 中任何规则的默认行为,因此实际上我们不用去考虑 Number 的作用,Ohm 会替我们来做这件事。 +第二,整数有一个参数然而浮点数有三个:a, b, 和 c. 这是由于规则的参数数量。参数数量意味着一个规则里面有多少参数。如果我们回过头去看语法,浮点数的规则是: ``` float = digit+ "." digit+ ``` - -The float rule is defined by three parts: the first digit+, the "." and the second digit+. All three of those parts will be passed as parameters to the action function for float. Thus float must have three arguments or else the Ohm library will give us an error. In this case we don’t care about the arguments because we will just grab the input string directly, but we still need the arguments listed to avoid compiler errors. Later on we will actually use some of these parameters. - -Now we can add a few more tests for our new floating point number support. - +浮点数规则通过三个部分来定义:第一个 digit+, '.', 还有第二个 digit+. 这三个部分都会作为参数传递给浮点数的作用函数。因此浮点数必须有三个参数否则 Ohm 库给出一个错误。在这种情况下我们不用在意参数因为我们仅仅直接攫取了输入串,但是我们仍然需要列表的参数来回避编译器错误。后面我们将实际使用其中一些参数。 +现在我们可以为新的浮点数支持添加更多的测试。 ``` test("123",123); test("999",999); @@ -162,15 +116,10 @@ test('123.456',123.456); test('0.123',0.123); test('.123',0.123); ``` - -Note that the last test will fail. A floating point number must begin with a digit, even if it’s just zero. .123 is not valid, and in fact the real JavaScript language has the same rule. - -### Hexadecimal - -So now we have integers and floats but there’s a few other number syntaxes that might be good to support: hexadecimal and scientific notation. Hex numbers are integers in base sixteen. The digits can be from 0 to 9 and A to F. Hex is often used in computer science when working with binary data because you can represent 0 through 255 exactly with only two digits. - -In most C derived programming languages (including JavaScript) hex numbers are preceded by `0x` to indicate to the compiler that what follows is a hexadecimal number. To support hex numbers in our parser we just need to add another rule. - +注意最后一个测试将会失败。一个浮点数必须以一个数开始,即使它仅仅是 0, .123 不是有效的,实际上真正的 JavaScript 语言有相同的规则。 +###十六进制数 +现在我们已经有了整数和浮点数,但是有一些其他的数的语法可能能够很好的支持:十六进制数和科学计数。十六进制数是是以 16 为基底2的数。十六进制数的数字能从 0 到 9 和从 A 到 F. 十六进制数经常用在计算机科学中当用二进制数据工作时,因为你可以仅仅使用两个数字表示 0 到 255 的数。 +在绝大多数源自 C 的编程语言(包括 JavaScript), 十六进制数通过在前面加上 ‘0x' 来向编译器表明后面跟的是一个十六进制数。为了让我们的解释器支持十六进制数,我们只需要添加另一条规则。 ``` Number = hex | float | int int = digit+ @@ -178,67 +127,49 @@ In most C derived programming languages (including JavaScript) hex numbers are p hex = "0x" hexDigit+ hexDigit := "0".."9" | "a".."f" | "A".."F" ``` - -I’ve actually added two rules. `hex` says that a hex number is the string `0x` followed by one or more `hexDigits`. A `hexDigit` is any letter from 0 to 9, or a to f, or A to F (covering both upper and lower case). I also modified Number to recognize hex as another possible option. Now we just need another action rule for hex. - +我实际上已经增加了两条规则。'hex' 表明十六进制数是一个 'ox' 后面一个或多个 ’hexDigits'[十六进制数子] 的串。一个 'hexDigit' 是从 0 到 9, 或从 a 到 f, 或 A 到 F(包扩大写和小写的情况)的一个字符。我也修改了 Number 规则来识别十六进制数作为其他可能的选择。现在我们只需要另一条针对十六进制数的作用规则。 ``` hex: function(a,b) { return parseInt(this.sourceString,16); } ``` +注意到,在这种情况下,我们把 '16' 作为基数传递给 'parseInt', 因为我们希望 JavaScript 知道这是一个十六进制数。 -Notice that in this case we are passing `16` as the radix to `parseInt` because we want JavaScript to know that this is a hexadecimal number. - -I skipped over something important to notice. The rule for `hexDigit` looks like this. - +我略过了一些很重要需要注意的事。针对 'hexDigit' 的规则像下面这样: ``` hexDigit := "0".."9" | "a".."f" | "A".."F" ``` - -Notice that I used `:=` instead of `=`. In Ohm, the `:=` is used when you are overriding a rule. It turns out Ohm already has a default rule for `hexDigit`, just as it does for `digit`, `space` and a bunch of others. If I had used = then Ohm would have reported an error. This is a check so I can’t override a rule unintentionally. Since our new hexDigit rule is actually the same as Ohm’s built in rule we can just comment it out and let Ohm do it. I left the rule in just so we can see what’s really going on. - +注意我使用的是 ':=' 而不是 '='. 在 Ohm 中,'=' 是当你需要推翻一条规则的时候使用。证明是 Ohm 已经有了针对 'hexDigit' 的默认规则,就像针对 'digit', 'space' 等一堆其他的东西。如果我使用了 '=', Ohm 将会报告一个错误。这是一个检查从而我不能无意识的推翻一个规则。由于新的 hexDigit 规则和 Ohm 的构建规则一样,所以我们可以仅仅对它添加注释然后让 Ohm 来实现它。我留下这个规则仅仅是因为这样我们可以看到它实际上是如何进行的。 Now we can add some more tests and see that our hex digits really work: - +现在,我们可以添加更多的测试然后看到十六进制数真的工作。 ``` test('0x456',0x456); test('0xFF',255); ``` - -### Scientific Notation - -Finally let’s support scientific notation. This is for very large or small numbers like 1.8 x 10^3 In most programming languages numbers in scientific notation would be written as 1.8e3 for 18000 or 1.8e-3 for .0018. Let’s add another couple of rules to support this exponent notation. - +###科学计数 +最后,让我们来支持科学计数。科学计数是针对非常大或非常小的数比如 1.8×10^3, 在大多数编程语言中,科学计数法表示的数会写成这样:1.8e3 表示 18000, 或者 1.8e-3 表示 .018. 让我们增加另外一对规则来支持这个指数表示: ``` float = digit+ "." digit+ exp? exp = "e" "-"? digit+ ``` - -This adds a the exp rule to the end of the float rule with a question mark. The ? means zero or one, so exp is optional but there can’t be more than one. Adding the exp rule also changes the arity of the float rule, so we need to add another argument to the float action, even if we don’t use it. - +上面增加了一个指数规则通过在浮点数规则末尾加上一个 '?'. '?' 表示 0 或 1,所以指数是可选择的但是不能超过一个。增加指数规则也改变了浮点数规则的参数数量,所以我们需要为浮点数作用增加又一个参数,即使我们不使用它。 ``` float: function(a,b,c,d) { console.log("doing float", this.sourceString); return parseFloat(this.sourceString); }, ``` - -And now our new tests can pass: - +现在我们的测试可以通过了: ``` test('4.8e10',4.8e10); test('4.8e-10',4.8e-10); ``` - -### Conclusion - -Ohm is a great tool for building parsers because it’s easy to get started and you can incrementally add to it. It also has other great features that I didn’t cover today, like a debugging visualizer and sub-classing. - -So far we have used Ohm to translate character strings into JavaScript numbers, and often Ohm is used for this very purpose: converting one representation to another. However, Ohm can be used for a lot more. By putting in a different set of semantic actions you can use Ohm to actually process and calculate things. This is one of Ohm’s magic features. A single grammar can be used with many different semantics. - -In the next article of this series I’ll show you how to not just parse numbers but actually evaluate math expressions like `(4.8+5 * (238-68)/2)`, just like a real calculator. - -Bonus challenge: Can you extend the grammar with support for octal numbers? These are numbers in base 8 and can be represented with only the digits 0 to 7, preceded by a zero and the letter o. See if you are right with these test cases. Next time I’ll show you the answer. - +###结论 +Ohm 是构建解释器的一个很棒的工具,因为它很容易开始并且你可以递增的增加规则。Ohm 也还有其他我今天没有写到的很棒的特点,比如故障观察仪和子类化。 +s. +到目前为止,我们已经使用 Ohm 来把字符串翻译成 JavaScript 数,并且 Ohm 经常由于需要把一个表示转化成另一个这一目的而使用。然而,Ohm 还有更多的用途。通过放入以系列不同的语义作用你可以使用 Ohm 来真正处理和计算东西。一个单独的语法可以被许多不同的语义使用,这是 Ohm 其中一个不可思议的特点。 +在这个系列的下一篇文章中,我将向你们展示如何计算像(4.85 + 5 * (238 - 68)/2) 这样的数学表达式,不仅仅是解析数。 +额外的挑战:你能够扩展语法来支持八进制数吗?这些以 8 为基底的数能够只用 0 到 7 这几个数字来表示,前面加上一个数字 0 或者字母 o. 看看针对下面这些测试情况是够正确。下次我将给出答案。 ``` test('0o77',7*8+7); test('0o23',0o23); @@ -252,7 +183,7 @@ test('0o23',0o23); via: https://www.pubnub.com/blog/2016-08-30-javascript-parser-ohm-makes-creating-a-programming-language-easy/?utm_source=javascriptweekly&utm_medium=email 作者:[Josh Marinacci][a] -译者:[译者ID](https://github.com/译者ID) +译者:[ucasFL](https://github.com/ucasFL) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From 241c446721a121765f828f1e257af3c70d4d2d41 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 22:53:42 +0800 Subject: [PATCH 06/48] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...hat Create a Language in 200 Lines of Code | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code diff --git a/translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code b/translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code new file mode 100644 index 0000000000..70dd8091a4 --- /dev/null +++ b/translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code @@ -0,0 +1,192 @@ +Ohm: 一种可以用两百行代码创造一种语言的 JavaScript 解释器 +解释器是一种非常有用的软件库。从概念上简单的说,它们的实现很有挑战并且在计算机科学中经常被认为是暗黑艺术。在这个系列的博文中,我会向你们展示为什么你不需要成为哈利波特就能够很好的控制解释器。但是为了以防万一带上你的魔杖。 +我们将探索一种叫做 Ohm 的新的开源库,它使得搭建解释器很简单并且更加容易再利用。在这个系列里,我们使用 Ohm 去识别数字,构建计算器等等。在这个系列的最后你将已经用少于 200 行的代码发明了一种完整的编程语言。这个强大的工具将让你能够做一些你可能过去认为不可能的事情。 +###为什么解释器很困难? +解释器非常有用。在很多时候你可能需要一个解释器。一种新的文件格式可能出现,你需要去处理但还没有人为它写了一个库;或者你发现了一种老格式的文件但是现存的解释器不能构建你需要的平台。我已经看到这样的事发生无数次。代码会来来去去但数据却是永恒的。 +基础的解释器很简单:只是把一个数据结构转化成另一个。所以为什么感觉你需要成为 邓布利多【魔法师】才能够把它们做出来。 +解释器的一些历史性的挑战是很难写,绝大多数工具很老并且假设了大量晦涩难懂的计算机科学知识。如果你在大学里上过编译器课程那么课本里可能也有从 1970 年以来的技术。幸运的是,解释器技术从那时候起已经提高了很多。 +代表性地,解释器是通过使用一种叫作形式语法的特殊语法来定义你想要解析的东西这样发明的,然后你需要把它放入像 Bison 和 Yacc 的工具中,这些工具能够产生一堆你需要修改的 C 代码或者链接到你实际写入额的编程语言。另外的选择是用你更喜欢的语言亲自动手写一个解释器,这很慢且很容易出错,在你能够真正使用它之前还有许多额外的工作。 +想像一下,是否你关于你想要解析的东西的语法描述也是解释器?如果你能够仅仅直接运行语法,然后在你需要的地方增加挂钩,那是什么?那就是 Ohm 所做的事。 +###解释器简介 +[Ohm][1]是一种新的解析系统。它类似你可能已经在课本里面看到的语法并且它更强大,使用起来更简单。通过 Ohm, 你能够使用一种灵活的语法以 .ohm 文件格式来写格式定义,然后使用你的宿主语言把语义加入到里面。在这篇博文里,我们将用 JavaScript 作为宿主语言。 +Ohm 建立在一个为制造更简单、更灵活的解释器的一个多年调查基础之上。VPRI 的 [STEPS program](pdf) 使用 Ohm 的前驱为许多特殊的工作创造了专门的语言(比如一个有 400 行代码的平行制图描绘器)[Ometa][3]. +Ohm 有许多有趣的特点和符号,但是不是要全部解释它们,我认为我们应该只需投入其中并构建一些东西。 +###解析整数 +让我们来解析一些数字。这看起来会很简单,只需在一个文本串中寻找毗邻的数字,但是让我们尝试去处理所有形式的数字:整数和浮点数,十六进制数和八进制数,科学计数,负数。解析数字很简单,正确解析却很难。 +亲自构建这个代码将会很困难,会有很多故障,会伴随有许多特殊的情况,比如有时会相互矛盾。 +用 Ohm 构建的解释器涉及三个部分:语法、语义和测试。我通常挑选一个问题的一部分为它写测试,然后构建足够的语法和语义来使测试通过。然后我再挑选问题的另一部分,增加更多的测试,更新语法和语义,从而确保所有的测试能够持续通过。即使我们有了新的强大的工具,写解释器从概念上来说依旧很困难。测试是用一种合理的方式来构建解释器的唯一方法。现在,让我们开始工作。 +我们将从整数开始。一个整数由一系列相互毗邻的数字组成。让我们把下面的内容放入一个叫做 grammar.ohm 的文件中: +``` +CoolNums { + // just a basic integer + Number = digit+ +} +``` +这创造了一条撮合一个或多个数字叫作 Number 的单一规则。+ 意味着一个或更多,就像一个常规的表达。当有一个或更多的数字时,这个规则将会撮合它们,如果没有数字或者有一些不是数字的东西将不会撮合。一个数字定义成从 0 到 9 其中的一个字符。数字也是像 Number 一样的规则,但是它是 Ohm 的其中一条构建规则因此我们不需要去定义它。我们可以推翻它如果我们想的话但在这时候这没有任何意义,毕竟我们不打算去发明一种新的数。 +现在,我们可以读入这个语法并用 Ohm 库来运行它。 +把它放入 test1.js +``` +var ohm = require('ohm-js'); +var fs = require('fs'); +var assert = require('assert'); +var grammar = ohm.grammar(fs.readFileSync('src/blog_numbers/syntax1.ohm').toString()); +``` +Ohm 的语法调用将把文件读入并解释成一个语法对象。现在我们可以增加一些语义。把下面内容增加到你的 JavaScript 文件中: +``` +var sem = grammar.createSemantics().addOperation('toJS', { + Number: function(a) { + return parseInt(this.sourceString,10); + } +}); +``` +这创造了一系列叫作 sem with the operation to JS[伴有 JavaScript 操作的语义] 的语义。这些语义至关重要,一群函数和语法中的每一条规则相匹配。一个函数将会被调用当与它相匹配的语法规则被解析时。上面的 Number 函数将会被调用当语法中的 Number 规则被解析时。语法定义在语言中 chunks[大块] 是什么,语义定义当 chunks[大块] 被解析时应该做什么。 +语义函数能够做我们想做的任何事,比如打印初故障信息,创造对象,或者递归调用 toJS 作用于任何子节点。此时我们仅仅想把文本转换成真正的 JavaScript 整数。 +所有的语义函数有一个包含一些有用性质的暗含对象。源属性代表输入文本和这个节点相匹配。这个 sourceString[源串] 是一个匹配输入串,调用构建在 JavaScript 中的parseInt 函数会把这个串转换成一个数。parseInt 中 10 这个参数告诉 JavaScript 我们输入的是一个以 10 为基底的数。如果少了这个参数, JavaScript 也会假定以 10 为基底,但是我们把它包含在里面因为后面我们将支持以 16 为基底的数,所以使之明确比较好。 +既然我们有一些语法,让我们来实际解析一些东西看一看我们的解释器是否能够工作。如果知道我们的解释器工作?通过测试它,许多许多的测试,每一个边缘情况都需要一个测试。 +伴随标准断言 API,有一个测试函数能够匹配一些输入并运用我们的语义把它转换成一个数,然后比较转换生成的数和我们期望的输入。 +``` + function test(input, answer) { + var match = grammar.match(input); + if(match.failed()) return console.log("input failed to match " + input + match.message); + var result = sem(match).toJS(); + assert.deepEqual(result,answer); + console.log('success = ', result, answer); + } +``` +这个函数就是上面这个。现在我们能够为不同的数写一堆测试。如果匹配失败我们的脚本将会丢弃一个例外。如果不能打印成功,让我们尝试一下,把下面这些内容加入到脚本中: +``` + test("123",123); + test("999",999); + test("abc",999); +``` +然后用节点 test.js 运行脚本 +你的输出应该是这样: +``` +success = 123 123 +success = 999 999 +input failed to match abcLine 1, col 1: +> 1 | abc + ^ +Expected a digit +``` +真酷。正如理所当然的那样,前两个成功了,第三个失败了。更好的是,Ohm 自动给了我们一个很棒的错误信息指出匹配失败。 +###浮点数 +我们的解释器工作了,但是它不能做任何非常有趣的事。让我们把它扩展成既能解析整数又能解析浮点数。改变 grammar.ohm 文件使它看起来像下面这样: +``` +CoolNums { + // just a basic integer + Number = float | int + int = digit+ + float = digit+ "." digit+ +} +``` +这把 Number 规则改变成指向一个浮点数或者一个整数。我的意思是,我们把这读成"一个 Number 由一个浮点数或者一个整数构成。”然后整数定义成 digit+, 浮点数定义成 digit+ 后面跟着一个句号然后再跟着另一个 digit+. 这意味着在句号前和句号后都至少要有一个数字。如果一个数中没有一个句号那么它就不是一个浮点数,因此就是一个整数。 +现在,让我们再次看一下我们的语义作用。由于我们现在有了新的规则所以我们需要新的作用函数:一个作为整数的,一个作为浮点数的。 +``` +var sem = grammar.createSemantics().addOperation('toJS', { + Number: function(a) { + return a.toJS(); + }, + int: function(a) { + console.log("doing int", this.sourceString); + return parseInt(this.sourceString,10); + }, + float: function(a,b,c) { + console.log("doing float", this.sourceString); + return parseFloat(this.sourceString); + } +}); +``` +这里有两件事情需要注意。首先,整数,浮点数和数都有相匹配的语法规则和函数。然而,针对数的作用不再有任何意义。它接收子节点 'a' 然后通过子节点返回 toJS 的结果。换句话说,Number 规则简单的返回相匹配的子规则。由于这是在 Ohm 中任何规则的默认行为,因此实际上我们不用去考虑 Number 的作用,Ohm 会替我们来做这件事。 +第二,整数有一个参数然而浮点数有三个:a, b, 和 c. 这是由于规则的参数数量。参数数量意味着一个规则里面有多少参数。如果我们回过头去看语法,浮点数的规则是: +``` + float = digit+ "." digit+ +``` +浮点数规则通过三个部分来定义:第一个 digit+, '.', 还有第二个 digit+. 这三个部分都会作为参数传递给浮点数的作用函数。因此浮点数必须有三个参数否则 Ohm 库给出一个错误。在这种情况下我们不用在意参数因为我们仅仅直接攫取了输入串,但是我们仍然需要列表的参数来回避编译器错误。后面我们将实际使用其中一些参数。 +现在我们可以为新的浮点数支持添加更多的测试。 +``` +test("123",123); +test("999",999); +//test("abc",999); +test('123.456',123.456); +test('0.123',0.123); +test('.123',0.123); +``` +注意最后一个测试将会失败。一个浮点数必须以一个数开始,即使它仅仅是 0, .123 不是有效的,实际上真正的 JavaScript 语言有相同的规则。 +###十六进制数 +现在我们已经有了整数和浮点数,但是有一些其他的数的语法可能能够很好的支持:十六进制数和科学计数。十六进制数是是以 16 为基底2的数。十六进制数的数字能从 0 到 9 和从 A 到 F. 十六进制数经常用在计算机科学中当用二进制数据工作时,因为你可以仅仅使用两个数字表示 0 到 255 的数。 +在绝大多数源自 C 的编程语言(包括 JavaScript), 十六进制数通过在前面加上 ‘0x' 来向编译器表明后面跟的是一个十六进制数。为了让我们的解释器支持十六进制数,我们只需要添加另一条规则。 +``` + Number = hex | float | int + int = digit+ + float = digit+ "." digit+ + hex = "0x" hexDigit+ + hexDigit := "0".."9" | "a".."f" | "A".."F" +``` +我实际上已经增加了两条规则。'hex' 表明十六进制数是一个 'ox' 后面一个或多个 ’hexDigits'[十六进制数子] 的串。一个 'hexDigit' 是从 0 到 9, 或从 a 到 f, 或 A 到 F(包扩大写和小写的情况)的一个字符。我也修改了 Number 规则来识别十六进制数作为其他可能的选择。现在我们只需要另一条针对十六进制数的作用规则。 +``` + hex: function(a,b) { + return parseInt(this.sourceString,16); + } +``` +注意到,在这种情况下,我们把 '16' 作为基数传递给 'parseInt', 因为我们希望 JavaScript 知道这是一个十六进制数。 + +我略过了一些很重要需要注意的事。针对 'hexDigit' 的规则像下面这样: +``` + hexDigit := "0".."9" | "a".."f" | "A".."F" +``` +注意我使用的是 ':=' 而不是 '='. 在 Ohm 中,'=' 是当你需要推翻一条规则的时候使用。证明是 Ohm 已经有了针对 'hexDigit' 的默认规则,就像针对 'digit', 'space' 等一堆其他的东西。如果我使用了 '=', Ohm 将会报告一个错误。这是一个检查从而我不能无意识的推翻一个规则。由于新的 hexDigit 规则和 Ohm 的构建规则一样,所以我们可以仅仅对它添加注释然后让 Ohm 来实现它。我留下这个规则仅仅是因为这样我们可以看到它实际上是如何进行的。 +Now we can add some more tests and see that our hex digits really work: +现在,我们可以添加更多的测试然后看到十六进制数真的工作。 +``` +test('0x456',0x456); +test('0xFF',255); +``` +###科学计数 +最后,让我们来支持科学计数。科学计数是针对非常大或非常小的数比如 1.8×10^3, 在大多数编程语言中,科学计数法表示的数会写成这样:1.8e3 表示 18000, 或者 1.8e-3 表示 .018. 让我们增加另外一对规则来支持这个指数表示: +``` + float = digit+ "." digit+ exp? + exp = "e" "-"? digit+ +``` +上面增加了一个指数规则通过在浮点数规则末尾加上一个 '?'. '?' 表示 0 或 1,所以指数是可选择的但是不能超过一个。增加指数规则也改变了浮点数规则的参数数量,所以我们需要为浮点数作用增加又一个参数,即使我们不使用它。 +``` + float: function(a,b,c,d) { + console.log("doing float", this.sourceString); + return parseFloat(this.sourceString); + }, +``` +现在我们的测试可以通过了: +``` +test('4.8e10',4.8e10); +test('4.8e-10',4.8e-10); +``` +###结论 +Ohm 是构建解释器的一个很棒的工具,因为它很容易开始并且你可以递增的增加规则。Ohm 也还有其他我今天没有写到的很棒的特点,比如故障观察仪和子类化。 +s. +到目前为止,我们已经使用 Ohm 来把字符串翻译成 JavaScript 数,并且 Ohm 经常由于需要把一个表示转化成另一个这一目的而使用。然而,Ohm 还有更多的用途。通过放入以系列不同的语义作用你可以使用 Ohm 来真正处理和计算东西。一个单独的语法可以被许多不同的语义使用,这是 Ohm 其中一个不可思议的特点。 +在这个系列的下一篇文章中,我将向你们展示如何计算像(4.85 + 5 * (238 - 68)/2) 这样的数学表达式,不仅仅是解析数。 +额外的挑战:你能够扩展语法来支持八进制数吗?这些以 8 为基底的数能够只用 0 到 7 这几个数字来表示,前面加上一个数字 0 或者字母 o. 看看针对下面这些测试情况是够正确。下次我将给出答案。 +``` +test('0o77',7*8+7); +test('0o23',0o23); +``` + + + + +-------------------------------------------------------------------------------- + +via: https://www.pubnub.com/blog/2016-08-30-javascript-parser-ohm-makes-creating-a-programming-language-easy/?utm_source=javascriptweekly&utm_medium=email + +作者:[Josh Marinacci][a] +译者:[ucasFL](https://github.com/ucasFL) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.pubnub.com/blog/author/josh/ +[1]: https://github.com/cdglabs/ohm +[2]: http://www.vpri.org/pdf/tr2012001_steps.pdf +[3]: http://tinlizzie.org/ometa/ + + From 1ec4e0e34002c65f02b93e69226aa8b9fb3f8ee7 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 16 Sep 2016 23:09:24 +0800 Subject: [PATCH 07/48] Delete 20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md --- ...Creates a Language in 200 Lines of Code.md | 196 ------------------ 1 file changed, 196 deletions(-) delete mode 100644 sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md diff --git a/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md b/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md deleted file mode 100644 index 810ba28c2a..0000000000 --- a/sources/tech/20160830 Ohm JavaScript Parser that Creates a Language in 200 Lines of Code.md +++ /dev/null @@ -1,196 +0,0 @@ -Translating by ucasFL - - -=========== -Ohm: 一种可以用两百行代码创造一种语言的 JavaScript 解释器 -解释器是一种非常有用的软件库。从概念上简单的说,它们的实现很有挑战并且在计算机科学中经常被认为是暗黑艺术。在这个系列的博文中,我会向你们展示为什么你不需要成为哈利波特就能够很好的控制解释器。但是为了以防万一带上你的魔杖。 -我们将探索一种叫做 Ohm 的新的开源库,它使得搭建解释器很简单并且更加容易再利用。在这个系列里,我们使用 Ohm 去识别数字,构建计算器等等。在这个系列的最后你将已经用少于 200 行的代码发明了一种完整的编程语言。这个强大的工具将让你能够做一些你可能过去认为不可能的事情。 -###为什么解释器很困难? -解释器非常有用。在很多时候你可能需要一个解释器。一种新的文件格式可能出现,你需要去处理但还没有人为它写了一个库;或者你发现了一种老格式的文件但是现存的解释器不能构建你需要的平台。我已经看到这样的事发生无数次。代码会来来去去但数据却是永恒的。 -基础的解释器很简单:只是把一个数据结构转化成另一个。所以为什么感觉你需要成为 邓布利多【魔法师】才能够把它们做出来。 -解释器的一些历史性的挑战是很难写,绝大多数工具很老并且假设了大量晦涩难懂的计算机科学知识。如果你在大学里上过编译器课程那么课本里可能也有从 1970 年以来的技术。幸运的是,解释器技术从那时候起已经提高了很多。 -代表性地,解释器是通过使用一种叫作形式语法的特殊语法来定义你想要解析的东西这样发明的,然后你需要把它放入像 Bison 和 Yacc 的工具中,这些工具能够产生一堆你需要修改的 C 代码或者链接到你实际写入额的编程语言。另外的选择是用你更喜欢的语言亲自动手写一个解释器,这很慢且很容易出错,在你能够真正使用它之前还有许多额外的工作。 -想像一下,是否你关于你想要解析的东西的语法描述也是解释器?如果你能够仅仅直接运行语法,然后在你需要的地方增加挂钩,那是什么?那就是 Ohm 所做的事。 -###解释器简介 -[Ohm][1]是一种新的解析系统。它类似你可能已经在课本里面看到的语法并且它更强大,使用起来更简单。通过 Ohm, 你能够使用一种灵活的语法以 .ohm 文件格式来写格式定义,然后使用你的宿主语言把语义加入到里面。在这篇博文里,我们将用 JavaScript 作为宿主语言。 -Ohm 建立在一个为制造更简单、更灵活的解释器的一个多年调查基础之上。VPRI 的 [STEPS program](pdf) 使用 Ohm 的前驱为许多特殊的工作创造了专门的语言(比如一个有 400 行代码的平行制图描绘器)[Ometa][3]. -Ohm 有许多有趣的特点和符号,但是不是要全部解释它们,我认为我们应该只需投入其中并构建一些东西。 -###解析整数 -让我们来解析一些数字。这看起来会很简单,只需在一个文本串中寻找毗邻的数字,但是让我们尝试去处理所有形式的数字:整数和浮点数,十六进制数和八进制数,科学计数,负数。解析数字很简单,正确解析却很难。 -亲自构建这个代码将会很困难,会有很多故障,会伴随有许多特殊的情况,比如有时会相互矛盾。 -用 Ohm 构建的解释器涉及三个部分:语法、语义和测试。我通常挑选一个问题的一部分为它写测试,然后构建足够的语法和语义来使测试通过。然后我再挑选问题的另一部分,增加更多的测试,更新语法和语义,从而确保所有的测试能够持续通过。即使我们有了新的强大的工具,写解释器从概念上来说依旧很困难。测试是用一种合理的方式来构建解释器的唯一方法。现在,让我们开始工作。 -我们将从整数开始。一个整数由一系列相互毗邻的数字组成。让我们把下面的内容放入一个叫做 grammar.ohm 的文件中: -``` -CoolNums { - // just a basic integer - Number = digit+ -} -``` -这创造了一条撮合一个或多个数字叫作 Number 的单一规则。+ 意味着一个或更多,就像一个常规的表达。当有一个或更多的数字时,这个规则将会撮合它们,如果没有数字或者有一些不是数字的东西将不会撮合。一个数字定义成从 0 到 9 其中的一个字符。数字也是像 Number 一样的规则,但是它是 Ohm 的其中一条构建规则因此我们不需要去定义它。我们可以推翻它如果我们想的话但在这时候这没有任何意义,毕竟我们不打算去发明一种新的数。 -现在,我们可以读入这个语法并用 Ohm 库来运行它。 -把它放入 test1.js -``` -var ohm = require('ohm-js'); -var fs = require('fs'); -var assert = require('assert'); -var grammar = ohm.grammar(fs.readFileSync('src/blog_numbers/syntax1.ohm').toString()); -``` -Ohm 的语法调用将把文件读入并解释成一个语法对象。现在我们可以增加一些语义。把下面内容增加到你的 JavaScript 文件中: -``` -var sem = grammar.createSemantics().addOperation('toJS', { - Number: function(a) { - return parseInt(this.sourceString,10); - } -}); -``` -这创造了一系列叫作 sem with the operation to JS[伴有 JavaScript 操作的语义] 的语义。这些语义至关重要,一群函数和语法中的每一条规则相匹配。一个函数将会被调用当与它相匹配的语法规则被解析时。上面的 Number 函数将会被调用当语法中的 Number 规则被解析时。语法定义在语言中 chunks[大块] 是什么,语义定义当 chunks[大块] 被解析时应该做什么。 -语义函数能够做我们想做的任何事,比如打印初故障信息,创造对象,或者递归调用 toJS 作用于任何子节点。此时我们仅仅想把文本转换成真正的 JavaScript 整数。 -所有的语义函数有一个包含一些有用性质的暗含对象。源属性代表输入文本和这个节点相匹配。这个 sourceString[源串] 是一个匹配输入串,调用构建在 JavaScript 中的parseInt 函数会把这个串转换成一个数。parseInt 中 10 这个参数告诉 JavaScript 我们输入的是一个以 10 为基底的数。如果少了这个参数, JavaScript 也会假定以 10 为基底,但是我们把它包含在里面因为后面我们将支持以 16 为基底的数,所以使之明确比较好。 -既然我们有一些语法,让我们来实际解析一些东西看一看我们的解释器是否能够工作。如果知道我们的解释器工作?通过测试它,许多许多的测试,每一个边缘情况都需要一个测试。 -伴随标准断言 API,有一个测试函数能够匹配一些输入并运用我们的语义把它转换成一个数,然后比较转换生成的数和我们期望的输入。 -``` - function test(input, answer) { - var match = grammar.match(input); - if(match.failed()) return console.log("input failed to match " + input + match.message); - var result = sem(match).toJS(); - assert.deepEqual(result,answer); - console.log('success = ', result, answer); - } -``` -这个函数就是上面这个。现在我们能够为不同的数写一堆测试。如果匹配失败我们的脚本将会丢弃一个例外。如果不能打印成功,让我们尝试一下,把下面这些内容加入到脚本中: -``` - test("123",123); - test("999",999); - test("abc",999); -``` -然后用节点 test.js 运行脚本 -你的输出应该是这样: -``` -success = 123 123 -success = 999 999 -input failed to match abcLine 1, col 1: -> 1 | abc - ^ -Expected a digit -``` -真酷。正如理所当然的那样,前两个成功了,第三个失败了。更好的是,Ohm 自动给了我们一个很棒的错误信息指出匹配失败。 -###浮点数 -我们的解释器工作了,但是它不能做任何非常有趣的事。让我们把它扩展成既能解析整数又能解析浮点数。改变 grammar.ohm 文件使它看起来像下面这样: -``` -CoolNums { - // just a basic integer - Number = float | int - int = digit+ - float = digit+ "." digit+ -} -``` -这把 Number 规则改变成指向一个浮点数或者一个整数。我的意思是,我们把这读成"一个 Number 由一个浮点数或者一个整数构成。”然后整数定义成 digit+, 浮点数定义成 digit+ 后面跟着一个句号然后再跟着另一个 digit+. 这意味着在句号前和句号后都至少要有一个数字。如果一个数中没有一个句号那么它就不是一个浮点数,因此就是一个整数。 -现在,让我们再次看一下我们的语义作用。由于我们现在有了新的规则所以我们需要新的作用函数:一个作为整数的,一个作为浮点数的。 -``` -var sem = grammar.createSemantics().addOperation('toJS', { - Number: function(a) { - return a.toJS(); - }, - int: function(a) { - console.log("doing int", this.sourceString); - return parseInt(this.sourceString,10); - }, - float: function(a,b,c) { - console.log("doing float", this.sourceString); - return parseFloat(this.sourceString); - } -}); -``` -这里有两件事情需要注意。首先,整数,浮点数和数都有相匹配的语法规则和函数。然而,针对数的作用不再有任何意义。它接收子节点 'a' 然后通过子节点返回 toJS 的结果。换句话说,Number 规则简单的返回相匹配的子规则。由于这是在 Ohm 中任何规则的默认行为,因此实际上我们不用去考虑 Number 的作用,Ohm 会替我们来做这件事。 -第二,整数有一个参数然而浮点数有三个:a, b, 和 c. 这是由于规则的参数数量。参数数量意味着一个规则里面有多少参数。如果我们回过头去看语法,浮点数的规则是: -``` - float = digit+ "." digit+ -``` -浮点数规则通过三个部分来定义:第一个 digit+, '.', 还有第二个 digit+. 这三个部分都会作为参数传递给浮点数的作用函数。因此浮点数必须有三个参数否则 Ohm 库给出一个错误。在这种情况下我们不用在意参数因为我们仅仅直接攫取了输入串,但是我们仍然需要列表的参数来回避编译器错误。后面我们将实际使用其中一些参数。 -现在我们可以为新的浮点数支持添加更多的测试。 -``` -test("123",123); -test("999",999); -//test("abc",999); -test('123.456',123.456); -test('0.123',0.123); -test('.123',0.123); -``` -注意最后一个测试将会失败。一个浮点数必须以一个数开始,即使它仅仅是 0, .123 不是有效的,实际上真正的 JavaScript 语言有相同的规则。 -###十六进制数 -现在我们已经有了整数和浮点数,但是有一些其他的数的语法可能能够很好的支持:十六进制数和科学计数。十六进制数是是以 16 为基底2的数。十六进制数的数字能从 0 到 9 和从 A 到 F. 十六进制数经常用在计算机科学中当用二进制数据工作时,因为你可以仅仅使用两个数字表示 0 到 255 的数。 -在绝大多数源自 C 的编程语言(包括 JavaScript), 十六进制数通过在前面加上 ‘0x' 来向编译器表明后面跟的是一个十六进制数。为了让我们的解释器支持十六进制数,我们只需要添加另一条规则。 -``` - Number = hex | float | int - int = digit+ - float = digit+ "." digit+ - hex = "0x" hexDigit+ - hexDigit := "0".."9" | "a".."f" | "A".."F" -``` -我实际上已经增加了两条规则。'hex' 表明十六进制数是一个 'ox' 后面一个或多个 ’hexDigits'[十六进制数子] 的串。一个 'hexDigit' 是从 0 到 9, 或从 a 到 f, 或 A 到 F(包扩大写和小写的情况)的一个字符。我也修改了 Number 规则来识别十六进制数作为其他可能的选择。现在我们只需要另一条针对十六进制数的作用规则。 -``` - hex: function(a,b) { - return parseInt(this.sourceString,16); - } -``` -注意到,在这种情况下,我们把 '16' 作为基数传递给 'parseInt', 因为我们希望 JavaScript 知道这是一个十六进制数。 - -我略过了一些很重要需要注意的事。针对 'hexDigit' 的规则像下面这样: -``` - hexDigit := "0".."9" | "a".."f" | "A".."F" -``` -注意我使用的是 ':=' 而不是 '='. 在 Ohm 中,'=' 是当你需要推翻一条规则的时候使用。证明是 Ohm 已经有了针对 'hexDigit' 的默认规则,就像针对 'digit', 'space' 等一堆其他的东西。如果我使用了 '=', Ohm 将会报告一个错误。这是一个检查从而我不能无意识的推翻一个规则。由于新的 hexDigit 规则和 Ohm 的构建规则一样,所以我们可以仅仅对它添加注释然后让 Ohm 来实现它。我留下这个规则仅仅是因为这样我们可以看到它实际上是如何进行的。 -Now we can add some more tests and see that our hex digits really work: -现在,我们可以添加更多的测试然后看到十六进制数真的工作。 -``` -test('0x456',0x456); -test('0xFF',255); -``` -###科学计数 -最后,让我们来支持科学计数。科学计数是针对非常大或非常小的数比如 1.8×10^3, 在大多数编程语言中,科学计数法表示的数会写成这样:1.8e3 表示 18000, 或者 1.8e-3 表示 .018. 让我们增加另外一对规则来支持这个指数表示: -``` - float = digit+ "." digit+ exp? - exp = "e" "-"? digit+ -``` -上面增加了一个指数规则通过在浮点数规则末尾加上一个 '?'. '?' 表示 0 或 1,所以指数是可选择的但是不能超过一个。增加指数规则也改变了浮点数规则的参数数量,所以我们需要为浮点数作用增加又一个参数,即使我们不使用它。 -``` - float: function(a,b,c,d) { - console.log("doing float", this.sourceString); - return parseFloat(this.sourceString); - }, -``` -现在我们的测试可以通过了: -``` -test('4.8e10',4.8e10); -test('4.8e-10',4.8e-10); -``` -###结论 -Ohm 是构建解释器的一个很棒的工具,因为它很容易开始并且你可以递增的增加规则。Ohm 也还有其他我今天没有写到的很棒的特点,比如故障观察仪和子类化。 -s. -到目前为止,我们已经使用 Ohm 来把字符串翻译成 JavaScript 数,并且 Ohm 经常由于需要把一个表示转化成另一个这一目的而使用。然而,Ohm 还有更多的用途。通过放入以系列不同的语义作用你可以使用 Ohm 来真正处理和计算东西。一个单独的语法可以被许多不同的语义使用,这是 Ohm 其中一个不可思议的特点。 -在这个系列的下一篇文章中,我将向你们展示如何计算像(4.85 + 5 * (238 - 68)/2) 这样的数学表达式,不仅仅是解析数。 -额外的挑战:你能够扩展语法来支持八进制数吗?这些以 8 为基底的数能够只用 0 到 7 这几个数字来表示,前面加上一个数字 0 或者字母 o. 看看针对下面这些测试情况是够正确。下次我将给出答案。 -``` -test('0o77',7*8+7); -test('0o23',0o23); -``` - - - - --------------------------------------------------------------------------------- - -via: https://www.pubnub.com/blog/2016-08-30-javascript-parser-ohm-makes-creating-a-programming-language-easy/?utm_source=javascriptweekly&utm_medium=email - -作者:[Josh Marinacci][a] -译者:[ucasFL](https://github.com/ucasFL) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://www.pubnub.com/blog/author/josh/ -[1]: https://github.com/cdglabs/ohm -[2]: http://www.vpri.org/pdf/tr2012001_steps.pdf -[3]: http://tinlizzie.org/ometa/ - - From 17610cc21fa11167cc5a3327335d4b7d853a0794 Mon Sep 17 00:00:00 2001 From: wxy Date: Fri, 16 Sep 2016 23:18:58 +0800 Subject: [PATCH 08/48] =?UTF-8?q?=E8=A1=A5=E5=AE=8C=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @ucasFL --- ...minism and increasing diversity in tech.md | 84 ------------------- ...inism and increasing diversity in tech.md} | 0 ...Create a Language in 200 Lines of Code.md} | 0 3 files changed, 84 deletions(-) delete mode 100644 sources/talk/20150806 Torvalds 2.0--Patricia Torvalds on computing college feminism and increasing diversity in tech.md rename translated/talk/{Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech => Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech.md} (100%) rename translated/tech/{20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code => 20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code.md} (100%) diff --git a/sources/talk/20150806 Torvalds 2.0--Patricia Torvalds on computing college feminism and increasing diversity in tech.md b/sources/talk/20150806 Torvalds 2.0--Patricia Torvalds on computing college feminism and increasing diversity in tech.md deleted file mode 100644 index 78c6cbaecf..0000000000 --- a/sources/talk/20150806 Torvalds 2.0--Patricia Torvalds on computing college feminism and increasing diversity in tech.md +++ /dev/null @@ -1,84 +0,0 @@ -LinuxBars translating - -Torvalds 2.0: Patricia Torvalds on computing, college, feminism, and increasing diversity in tech -================================================================================ - -![Image by : Photo by Becky Svartström. Modified by Opensource.com. CC BY-SA 4.0](http://opensource.com/sites/default/files/styles/image-full-size/public/images/life/osdc-lead-patriciatorvalds.png) -Image by : Photo by Becky Svartström. Modified by Opensource.com. [CC BY-SA 4.0][1] - -Patricia Torvalds isn't the Torvalds name that pops up in Linux and open source circles. Yet. - -![](http://opensource.com/sites/default/files/images/life-uploads/ptorvalds.png) - -At 18, Patricia is a feminist with a growing list of tech achievements, open source industry experience, and her sights set on diving into her freshman year of college at Duke University's Pratt School of Engineering. She works for [Puppet Labs][2] in Portland, Oregon, as an intern, but soon she'll head to Durham, North Carolina, to start the fall semester of college. - -In this exclusive interview, Patricia explains what got her interested in computer science and engineering (spoiler alert: it wasn't her father), what her high school did "right" with teaching tech, the important role feminism plays in her life, and her thoughts on the lack of diversity in technology. - -![](http://opensource.com/sites/default/files/images/life/Interview%20banner%20Q%26A.png) - -### What made you interested in studying computer science and engineering? ### - -My interest in tech really grew throughout high school. I wanted to go into biology for a while, until around my sophomore year. I had a web design internship at the Portland VA after my sophomore year. And I took an engineering class called Exploratory Ventures, which sent an ROV into the Pacific ocean late in my sophomore year, but the turning point was probably when I was named a regional winner and national runner up for the [NCWIT Aspirations in Computing][3] award halfway through my junior year. - -The award made me feel validated in my interest, of course, but I think the most important part of it was getting to join a Facebook group for all the award winners. The girls who have won the award are absolutely incredible and so supportive of each other. I was definitely interested in computer science before I won the award, because of my work in XV and at the VA, but having these girls to talk to solidified my interest and has kept it really strong. Teaching XV—more on that later—my junior and senior year, also, made engineering and computer science really fun for me. - -### What do you plan to study? And do you already know what you want to do after college? ### - -I hope to major in either Mechanical or Electrical and Computer Engineering as well as Computer Science, and minor in Women's Studies. After college, I hope to work for a company that supports or creates technology for social good, or start my own company. - -### My daughter had one high school programming class—Visual Basic. She was the only girl in her class, and she ended up getting harassed and having a miserable experience. What was your experience like? ### - -My high school began offering computer science classes my senior year, and I took Visual Basic as well! The class wasn't bad, but I was definitely one of three or four girls in the class of 20 or so students. Other computing classes seemed to have similar gender breakdowns. However, my high school was extremely small and the teacher was supportive of inclusivity in tech, so there was no harassment that I noticed. Hopefully the classes become more diverse in future years. - -### What did your schools do right technology-wise? And how could they have been better? ### - -My high school gave us consistent access to computers, and teachers occasionally assigned technology-based assignments in unrelated classes—we had to create a website for a social studies class a few times—which I think is great because it exposes everyone to tech. The robotics club was also pretty active and well-funded, but fairly small; I was not a member. One very strong component of the school's technology/engineering program is actually a student-taught engineering class called Exploratory Ventures, which is a hands-on class that tackles a new engineering or computer science problem every year. I taught it for two years with a classmate of mine, and have had students come up to me and tell me they're interested in pursuing engineering or computer science as a result of the class. - -However, my high school was not particularly focused on deliberately including young women in these programs, and it isn't very racially diverse. The computing-based classes and clubs were, by a vast majority, filled with white male students. This could definitely be improved on. - -### Growing up, how did you use technology at home? ### - -Honestly, when I was younger I used my computer time (my dad created a tracker, which logged us off after an hour of Internet use) to play Neopets or similar games. I guess I could have tried to mess with the tracker or played on the computer without Internet use, but I just didn't. I sometimes did little science projects with my dad, and I remember once printing "Hello world" in the terminal with him a thousand times, but mostly I just played online games with my sisters and didn't get my start in computing until high school. - -### You were active in the Feminism Club at your high school. What did you learn from that experience? What feminist issues are most important to you now? ### - -My friend and I co-founded Feminism Club at our high school late in our sophomore year. We did receive lots of resistance to the club at first, and while that never entirely went away, by the time we graduated feminist ideals were absolutely a part of the school's culture. The feminist work we did at my high school was generally on a more immediate scale and focused on issues like the dress code. - -Personally, I'm very focused on intersectional feminism, which is feminism as it applies to other aspects of oppression like racism and classism. The Facebook page [Guerrilla Feminism][4] is a great example of an intersectional feminism and has done so much to educate me. I currently run the Portland branch. - -Feminism is also important to me in terms of diversity in tech, although as an upper-class white woman with strong connections in the tech world, the problems here affect me much less than they do other people. The same goes for my involvement in intersectional feminism. Publications like [Model View Culture][5] are very inspiring to me, and I admire Shanley Kane so much for what she does. - -### What advice would you give parents who want to teach their children how to program? ### - -Honestly, nobody ever pushed me into computer science or engineering. Like I said, for a long time I wanted to be a geneticist. I got a summer internship doing web design for the VA the summer after my sophomore year and totally changed my mind. So I don't know if I can fully answer that question. - -I do think genuine interest is important, though. If my dad had sat me down in front of the computer and told me to configure a webserver when I was 12, I don't think I'd be interested in computer science. Instead, my parents gave me a lot of free reign to do what I wanted, which was mostly coding terrible little HTML sites for my Neopets. Neither of my younger sisters are interested in engineering or computer science, and my parents don't care. I'm really lucky my parents have given me and my sisters the encouragement and resources to explore our interests. - -Still, I grew up saying my future career would be "like my dad's"—even when I didn't know what he did. He has a pretty cool job. Also, one time when I was in middle school, I told him that and he got a little choked up and said I wouldn't think that in high school. So I guess that motivated me a bit. - -### What suggestions do you have for leaders in open source communities to help them attract and maintain a more diverse mix of contributors? ### - -I'm actually not active in particular open source communities. I feel much more comfortable discussing computing with other women; I'm a member of the [NCWIT Aspirations in Computing][6] network and it's been one of the most important aspects of my continued interest in technology, as well as the Facebook group [Ladies Storm Hackathons][7]. - -I think this applies well to attracting and maintaining a talented and diverse mix of contributors: Safe spaces are important. I have seen the misogynistic and racist comments made in some open source communities, and subsequent dismissals when people point out the issues. I think that in maintaining a professional community there have to be strong standards on what constitutes harassment or inappropriate conduct. Of course, people can—and will—have a variety of opinions on what they should be able to express in open source communities, or any community. However, if community leaders actually want to attract and maintain diverse talent, they need to create a safe space and hold community members to high standards. - -I also think that some community leaders just don't value diversity. It's really easy to argue that tech is a meritocracy, and the reason there are so few marginalized people in tech is just that they aren't interested, and that the problem comes from earlier on in the pipeline. They argue that if someone is good enough at their job, their gender or race or sexual orientation doesn't matter. That's the easy argument. But I was raised not to make excuses for mistakes. And I think the lack of diversity is a mistake, and that we should be taking responsibility for it and actively trying to make it better. - --------------------------------------------------------------------------------- - -via: http://opensource.com/life/15/8/patricia-torvalds-interview - -作者:[Rikki Endsley][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:http://opensource.com/users/rikki-endsley -[1]:https://creativecommons.org/licenses/by-sa/4.0/ -[2]:https://puppetlabs.com/ -[3]:https://www.aspirations.org/ -[4]:https://www.facebook.com/guerrillafeminism -[5]:https://modelviewculture.com/ -[6]:https://www.aspirations.org/ -[7]:https://www.facebook.com/groups/LadiesStormHackathons/ diff --git a/translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech b/translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech.md similarity index 100% rename from translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech rename to translated/talk/Torvalds2.0: Patricia Torvalds on computing, college, feminism and increasing diversity in tech.md diff --git a/translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code b/translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code.md similarity index 100% rename from translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code rename to translated/tech/20160830 Ohm JavaScript Parser that Create a Language in 200 Lines of Code.md From bec76212d6935164ff7d6bc133aa0d7e5f11e283 Mon Sep 17 00:00:00 2001 From: qingyun <845767657@qq.com> Date: Sat, 17 Sep 2016 12:53:29 +0800 Subject: [PATCH 09/48] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90=2020?= =?UTF-8?q?160917=20A=20Web=20Crawler=20With=20asyncio=20Coroutines.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7 A Web Crawler With asyncio Coroutines.md | 1088 +++++++++++++++++ 1 file changed, 1088 insertions(+) create mode 100644 translated/tech/20160917 A Web Crawler With asyncio Coroutines.md diff --git a/translated/tech/20160917 A Web Crawler With asyncio Coroutines.md b/translated/tech/20160917 A Web Crawler With asyncio Coroutines.md new file mode 100644 index 0000000000..1bce0fd22a --- /dev/null +++ b/translated/tech/20160917 A Web Crawler With asyncio Coroutines.md @@ -0,0 +1,1088 @@ +一个使用asyncio协程的网络爬虫 +=== + + +## 介绍 + +经典的计算机科学强调高效的算法,尽可能快地完成计算。但是很多网络程序的时间并不是消耗在计算上,而是在等待许多慢速的连接或者低频事件的发生。这些程序暴露出一个新的挑战:如何高效的等待大量网络事件。一个现代的解决方案是异步I/O。 + +这一章我们将实现一个简单的网络爬虫。这个爬虫只是一个原型式的异步应用,因为它等待许多响应而只做少量的计算。一次爬的网页越多,它就能越快的完成任务。如果它为每个动态的请求启动一个线程的话,随着并发请求数量的增加,它会在耗尽套接字之前,耗尽内存或者线程相关的资源。使用异步I/O可以避免这个的问题。 + +我们将分三个阶段展示这个例子。首先,我们会实现一个事件循环并用这个事件循环和回调来勾画出一个网络爬虫。它很有效,但是当把它扩展成更复杂的问题时,就会导致无法管理的混乱代码。然后,由于Python的协程不仅有效而且可扩展,我们将用Python的生成器函数实现一个简单的协程。在最后一个阶段,我们将使用Python标准库"asyncio"中功能完整的协程和异步队列完成这个网络爬虫。 + +## 任务 + +网络爬虫寻找并下载一个网站上的所有网页,也许还会把它们存档,为它们建立索引。从根URL开始,它获取每个网页,解析出没有遇到过的链接加到队列中。当网页没有未见到过的链接并且队列为空时,它便停止运行。 + +我们可以通过同时下载大量的网页来加快这一过程。当爬虫发现新的链接,它使用一个新的套接字并行的处理这个新链接,解析响应,添加新链接到队列。当并发很大时,可能会导致性能下降,所以我们会限制并发的数量,在队列保留那些未处理的链接,直到一些正在执行的任务完成。 + +## 传统方式 + +怎么使一个爬虫并发?传统的做法是创建一个线程池,每个线程使用一个套接字在一段时间内负责一个网页的下载。比如,下载xkcd.com网站的一个网页: + +```python +def fetch(url): + sock = socket.socket() + sock.connect(('xkcd.com', 80)) + request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(url) + sock.send(request.encode('ascii')) + response = b'' + chunk = sock.recv(4096) + while chunk: + response += chunk + chunk = sock.recv(4096) + + # Page is now downloaded. + links = parse_links(response) + q.add(links) +``` + +套接字操作默认是阻塞的:当一个线程调用一个类似`connect`和`recv`方法时,它会阻塞,直到操作完成.[^15]因此,为了同一时间内下载多个网页,我们需要很多线程。一个复杂的应用会通过线程池保持空闲的线程来分摊创建线程的开销。同样的做法也适用于套接字,使用连接池。 + +到目前为止,线程是昂贵的,操作系统对一个进程,一个用户,一台机器能使用线程做了不同的硬性限制。在Jesse系统中,一个Python线程需要50K的内存,开启上万个线程会失败。每个线程的开销和系统的限制就是这种方式的瓶颈所在。 + +在Dan Kegel那一篇很有影响力的文章"The C10K problem"[^8]中,它提出多线程方式在I/O并发上的局限性。他在开始写道, + +>是时候网络服务器要同时处理成千上万的客户啦,你不这样认为么?毕竟,现在网络是个很大的地方。 + +Kegel在1999年创造出"C10K"术语。一万个连接在今天看来还是可接受的,但是问题依然存在,只不过大小不同。回到那时候,对于C10K问题,每个连接启一个线程是不切实际的。现在这个限制已经成指数级增长。确实,我们的玩具网络爬虫使用线程也可以工作的很好。但是,对于有着千万级连接的大规模应用来说,限制依然存在:会消耗掉所有线程,即使套接字还够用。那么我们该如何解决这个问题? + +## 异步 + +异步I/O框架在一个线程中完成并发操作。让我们看看这是怎么做到的。 + +异步框架使用*非阻塞*套接字。异步爬虫中,我们在发起到服务器的连接前把套接字设为非阻塞: + +```python +sock = socket.socket() +sock.setblocking(False) +try: + sock.connect(('xkcd.com', 80)) +except BlockingIOError: + pass +``` + +对一个非阻塞套接字调用`connect`方法会立即抛出异常,即使它正常工作。这个异常模拟了底层C语言函数的行为,它把`errno`设置为`EINPROGRESS`,告诉你操作已经开始。 + +现在我们的爬虫需要一种知道连接何时建立的方法,这样它才能发送HTTP请求。我们可以简单地使用循环来重试: + +```python +request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(url) +encoded = request.encode('ascii') + +while True: + try: + sock.send(encoded) + break # Done. + except OSError as e: + pass + +print('sent') +``` + +这种方法不仅消耗CPU,也不能有效的等待*多个*套接字。在远古时代,BSD Unix的解决方法是`select`,一个C函数,它在一个或一组非阻塞套接字上等待事件发生。现在,互联网应用大量连接的需求,导致`select`被`poll`代替,以及BSD的`kqueue`和Linux的`epoll`。它们的API和`select`相似,但在大数量的连接中也能有较好的性能。 + +Python 3.4的`DefaultSelector`使用你系统上最好的类`select`函数。去注册一个网络I/O事件,我们创建一个非阻塞套接字,并使用默认的selector注册。 + +```python +from selectors import DefaultSelector, EVENT_WRITE + +selector = DefaultSelector() + +sock = socket.socket() +sock.setblocking(False) +try: + sock.connect(('xkcd.com', 80)) +except BlockingIOError: + pass + +def connected(): + selector.unregister(sock.fileno()) + print('connected!') + +selector.register(sock.fileno(), EVENT_WRITE, connected) +``` + +我们不理会这个伪造的错误,调用`selector.register`,传递套接字文件描述符,一个表示我们想要监听什么事件的常量。为了当连接建立时收到提醒,我们使用`EVENT_WRITE`:它表示什么时候这个套接字可写。我们还传递了一个Python函数,`connected`,当对应事件发生时被调用。这样的函数被称为*回调*。 + +我们在一个循环中处理I/O提醒,随着selector接收到它们。 + +```python +def loop(): + while True: + events = selector.select() + for event_key, event_mask in events: + callback = event_key.data + callback() +``` + +`connected`回调函数被保存在`event_key.data`中,一旦这个非阻塞套接字建立连接,它就会被取出来执行。 + +不像我们前面那个快速重试的循环,这里的`select`调用会阻塞,等待下一个I/O事件,接着执行等待这个事件的回调函数。 + +到目前为止我们展现了什么?我们展示了如何开始一个I/O操作和当操作准备好时调用回调函数。异步*框架*,它在单线程中执行并发操作,建立在两个功能之上,非阻塞套接字和事件循环。 + +## 回调 + +用我们刚刚建立的异步框架,怎么才能完成一个网络爬虫?即使是一个简单的网页下载程序也是很难写的。 + +首先,我们有一个未获取的URL集合,和一个已经解析过的URL集合。 + +```python +urls_todo = set(['/']) +seen_urls = set(['/']) +``` + +这两个集合加在一起就是所有的URL。用"/"初始化它们。 + +获取一个网页需要一系列的回调。在套接字连接建立时`connected`回调触发,它向服务器发送一个GET请求。但是它要等待响应,所以我们需要注册另一个回调函数,当回调被调用,它也不能一次读取完整的请求,所以,需要再一次注册,如此反复。 + +让我们把这些回调放在一个`Fetcher`对象中,它需要一个URL,一个套接字,还需要一个地方保存返回的字节: + +```python +class Fetcher: + def __init__(self, url): + self.response = b'' # Empty array of bytes. + self.url = url + self.sock = None +``` + +我们的入口点在`Fetcher.fetch`: + +```python + # Method on Fetcher class. + def fetch(self): + self.sock = socket.socket() + self.sock.setblocking(False) + try: + self.sock.connect(('xkcd.com', 80)) + except BlockingIOError: + pass + + # Register next callback. + selector.register(self.sock.fileno(), + EVENT_WRITE, + self.connected) +``` + +`fetch`方法从连接一个套接字开始。但是要注意这个方法在连接建立前就返回了。它必须返回到事件循环中等待连接建立。为了理解为什么要要这样,假设我们程序的整体结构如下: + +```python +# Begin fetching http://xkcd.com/353/ +fetcher = Fetcher('/353/') +fetcher.fetch() + +while True: + events = selector.select() + for event_key, event_mask in events: + callback = event_key.data + callback(event_key, event_mask) +``` + +所有的事件提醒都在事件循环中的`select`函数后处理。所以`fetch`必须把控制权交给事件循环。这样我们的程序才能知道什么时候连接已建立,接着循环调用`connected`回调,它已经在`fetch`方法中注册过。 + +这里是我们`connected`方法的实现: + +```python + # Method on Fetcher class. + def connected(self, key, mask): + print('connected!') + selector.unregister(key.fd) + request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(self.url) + self.sock.send(request.encode('ascii')) + + # Register the next callback. + selector.register(key.fd, + EVENT_READ, + self.read_response) +``` + +这个方法发送一个GET请求。一个真正的应用会检查`send`的返回值,以防所有的信息没能一次发送出去。但是我们的请求很小,应用也不复杂。它只是简单的调用`send`,然后等待响应。当然,它必须注册另一个回调并把控制权交给事件循环。接下来也是最后一个回调函数`read_response`,它处理服务器的响应: + +```python + # Method on Fetcher class. + def read_response(self, key, mask): + global stopped + + chunk = self.sock.recv(4096) # 4k chunk size. + if chunk: + self.response += chunk + else: + selector.unregister(key.fd) # Done reading. + links = self.parse_links() + + # Python set-logic: + for link in links.difference(seen_urls): + urls_todo.add(link) + Fetcher(link).fetch() # <- New Fetcher. + + seen_urls.update(links) + urls_todo.remove(self.url) + if not urls_todo: + stopped = True +``` + +这个回调在每次selector发现套接字*可读*时被调用,可读有两种情况:套接字接受到数据或它被关闭。 + +这个回调函数从套接字读取4K数据。如果没有4k,那么有多少读多少。如果比4K多,`chunk`只包4K数据并且这个套接字保持可读,这样在事件循环的下一个周期,会在次回到这个回调函数。当响应完成时,服务器关闭这个套接字,`chunk`为空。 + +没有展示的`parse_links`方法,它返回一个URL集合。我们为每个新的URL启动一个fetcher。注意一个使用异步回调方式编程的好处:我们不需要为共享数据加锁,比如我们往`seen_urls`增加新链接时。这是一种非抢占式的多任务,它不会在我们代码中的任意一个地方中断。 + +我们增加了一个全局变量`stopped`,用它来控制这个循环: + +```python +stopped = False + +def loop(): + while not stopped: + events = selector.select() + for event_key, event_mask in events: + callback = event_key.data + callback() +``` + +一旦所有的网页被下载下来,fetcher停止这个事件循环,程序退出。 + +这个例子让异步编程的一个问题明显的暴露出来:意大利面代码。 + +我们需要某种方式来表达一串计算和I/O操作,并且能够调度多个这样的操作让他们并发的执行。但是,没有线程你不能把这一串操作写在一个函数中:当函数开始一个I/O操作,它明确的把未来所需的状态保存下来,然后返回。你需要考虑如何写这个状态保存的代码。 + +让我们来解释下这到底是什么意思。考虑在线程中使用通常的阻塞套接字来获取一个网页时是多么简单。 + +```python +# Blocking version. +def fetch(url): + sock = socket.socket() + sock.connect(('xkcd.com', 80)) + request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(url) + sock.send(request.encode('ascii')) + response = b'' + chunk = sock.recv(4096) + while chunk: + response += chunk + chunk = sock.recv(4096) + + # Page is now downloaded. + links = parse_links(response) + q.add(links) +``` + +在一个套接字操作和下一个操作之间这个函数到底记住了什么?它有一个套接字,一个URL和一个可增长的`response`。运行在线程中的函数使用编程语言的基本功能,栈中的局部变量来保存临时的状态。这样的函数有一个"continuation"----在I/O结束后它要执行的代码。运行时通过线程的指令指针来记住这个continuation。你不必考虑怎么在I/O操作后恢复局部变量和这个continuation。语言本身的特性帮你解决。 + +但是用一个基于回调的异步框架,这些语言特性不能提供一点帮助。当等待I/O操作时,一个函数必须明确的保存它的状态,因为它会在I/O操作完成之前返回并清除栈帧。为了在我们基于回调的例子中代替局部变量,我们把`sock`和`response`作为Fetcher实例`self`属性。为了代替指令指针,它通过注册`connnected`和`read_response`回调来保存continuation。随着应用功能的增长,我们手动保存回调的复杂性也会增加。如此繁复的记账式工作会让编码者感到头痛。 + +更糟糕的是,当我们的回调函数抛出异常会发生什么?假设我们没有写好`parse_links`方法,它在解析HTML时抛出异常: + +``` +Traceback (most recent call last): + File "loop-with-callbacks.py", line 111, in + loop() + File "loop-with-callbacks.py", line 106, in loop + callback(event_key, event_mask) + File "loop-with-callbacks.py", line 51, in read_response + links = self.parse_links() + File "loop-with-callbacks.py", line 67, in parse_links + raise Exception('parse error') +Exception: parse error +``` + +这个堆栈回溯只能显示出事件循环调用了一个回调。我们不知道是什么导致了这个错误。这条链的两边都被破坏:不知道从哪来也不知到哪去。这种丢失上下文的现象被称为"stack ripping",它还会阻止我们为回调链设置异常处理。 + +所以,除了关于多线程和异步那个更高效的争议,还有一个关于这两者之间的争论,谁更容易出错。如果在同步上出现失误,线程更容易出现数据竞争的问题,而回调因为"stack ripping"问题而非常难于调试。 + +## 协程 + +还记得我们对你许下的承诺么?我们可以写出这样的异步代码,它既有回调方式的高效,也有多线程代码的简洁。这个结合是同过一种称为协程的模式来实现的。使用Python3.4标准库asyncio和一个叫"aiohttp"的包,在协程中获取一个网页是非常直接的[^10]: + +```python + @asyncio.coroutine + def fetch(self, url): + response = yield from self.session.get(url) + body = yield from response.read() +``` + +它也是可扩展的。在Jesse系统上,与每个线程50k内存相比,一个Python协程只需要3k内存。Python很容易就可以启动上千个协程。 + +协程的概念可以追溯到计算机科学的远古时代,它很简单,一个可以暂停和恢复的子过程。线程是被操作系统控制的抢占式多任务,而协程是可合作的,它们自己选择什么时候暂停去执行下一个协程。 + +有很多协程的实现。甚至在Python中也有几种。Python3.4标准库asyncio中的协程,它是建立在生成器,一个Future类和"yield from"语句之上。从Python3.5开始,协程变成了语言本身的特性。然而,理解Python3.4中这个通过语言原有功能实现的协程,是我们处理Python3.5中原生协程的基础。 + +## 生成器如何工作 + +在你理解生成器之前,你需要知道普通的Python函数是怎么工作的。当一个函数调用一个子过程,这个被调用函数获得控制权。直到它返回或者有异常发生,才把控制权交给调用者: + +```python +>>> def foo(): +... bar() +... +>>> def bar(): +... pass +``` + +标准的Python解释器是C语言写的。一个Python函数被调用对应的C函数是`PyEval_EvalFrameEx`。它获得一个Python栈帧结构并在这个栈帧的上下文中执行Python字节码。这里是`foo`的字节码: + +```python +>>> import dis +>>> dis.dis(foo) + 2 0 LOAD_GLOBAL 0 (bar) + 3 CALL_FUNCTION 0 (0 positional, 0 keyword pair) + 6 POP_TOP + 7 LOAD_CONST 0 (None) + 10 RETURN_VALUE +``` + +`foo`函数在它栈中加载`bar`并调用它,然后把`bar`的返回值从栈中弹出,加载`None`值并返回。 + +当`PyEval_EvalFrameEx`遇到`CALL_FUNCTION`字节码时,它会创建一个新的栈帧,并用这个栈帧递归的调用`PyEval_EvalFrameEx`来执行`bar`函数。 + +图 + +非常重要的一点是,Python的栈帧在堆中分配!Python解释器是一个标准的C程序,所以他的栈帧是正常的栈帧。但是Python的栈帧是在堆中处理。这意味着Python栈帧在函数调用结束后依然可以存在。我们在`bar`函数中保存当前的栈帧,交互式的看看这种现象: + +```python +>>> import inspect +>>> frame = None +>>> def foo(): +... bar() +... +>>> def bar(): +... global frame +... frame = inspect.currentframe() +... +>>> foo() +>>> # The frame was executing the code for 'bar'. +>>> frame.f_code.co_name +'bar' +>>> # Its back pointer refers to the frame for 'foo'. +>>> caller_frame = frame.f_back +>>> caller_frame.f_code.co_name +'foo' +``` + +现在该说Python生成器了,它使用同样构件--code object和栈帧--去完成一个不可思议的任务。 + +这是一个生成器函数: + +```python +>>> def gen_fn(): +... result = yield 1 +... print('result of yield: {}'.format(result)) +... result2 = yield 2 +... print('result of 2nd yield: {}'.format(result2)) +... return 'done' +... +``` + +在Python把`gen_fn`编译成字节码的过程中,一旦它看到`yield`语句就知道这是一个生成器函数而不是普通的函数。它就会设置一个标志来记住这个事实: + +```python +>>> # The generator flag is bit position 5. +>>> generator_bit = 1 << 5 +>>> bool(gen_fn.__code__.co_flags & generator_bit) +True +``` + +当你调用一个生成器函数,Python看到这个标志,就不会运行它而是创建一个生成器: + +```python +>>> gen = gen_fn() +>>> type(gen) + +``` + +Python生成器封装了一个栈帧和函数体代码: + +```python +>>> gen.gi_code.co_name +'gen_fn' +``` + +所有通过调用`gen_fn`的生成器指向同一段代码,但都有各自的栈帧。这些栈帧不再任何一个C函数栈中,而是在堆空间中等待被使用: + +图 + +栈帧中有一个指向最后执行指令的指针。初始化为-1,意味着它没开始运行: + +```python +>>> gen.gi_frame.f_lasti +-1 +``` + +当我们调用`send`时,生成器一直运行到第一个`yield`语句处停止。并且`send`返回1,yield语句后的表达式的值。 + +```python +>>> gen.send(None) +1 +``` + +现在生成器的指令指针是3,字节码一共有56个字节: + +```python +>>> gen.gi_frame.f_lasti +3 +>>> len(gen.gi_code.co_code) +56 +``` + +这个生成器可以在任何时候,任何函数中恢复运行,因为它的栈帧并不在真正的栈中,而是堆中。在调用链中它的位置也是不确定的,它不必遵循普通函数先进后出的顺序。它像云一样自由。 + +我们可以传递一个`hello`给生成器,它会成为yield语句的结果,并且生成器运行到第二个yield语句处。 + +```python +>>> gen.send('hello') +result of yield: hello +2 +``` + +现在栈帧中包含局部变量`result`: + +```python +>>> gen.gi_frame.f_locals +{'result': 'hello'} +``` + +其它从`gen_fn`创建的生成器有着它自己的栈帧和局部变量。 + +当我们在一次调用`send`,生成器从第二个yield开始运行,以抛出一个特殊的`StopIteration`异常为结束。 + +```python +>>> gen.send('goodbye') +result of 2nd yield: goodbye +Traceback (most recent call last): + File "", line 1, in +StopIteration: done +``` + +这个异常有一个值"done",它就是生成器的返回值。 + +## 使用生成器构建协程 + +所以生成器可以暂停,可以给它一个值让它恢复,并且它还有一个返回值。这些特性看起来很适合去建立一个不使用回调的异步编程模型。我们想创造一个协程:一个在程序中可以和其他过程合作调度的过程。我们的协程将会是标准库`asyncio`中协程的一个简化版本,我们将使用生成器,futures和`yield from`语句。 + +首先,我们需要一种方法去代表协程需要等待的未来事件。一个简化的版本是: + +```python +class Future: + def __init__(self): + self.result = None + self._callbacks = [] + + def add_done_callback(self, fn): + self._callbacks.append(fn) + + def set_result(self, result): + self.result = result + for fn in self._callbacks: + fn(self) +``` + +一个future初始化为未解决的,它同过调用`set_result`来解决。[^12] + +让我们用futures和协程来改写我们的fetcher。我们之前用回调写的fetcher如下: + +```python +class Fetcher: + def fetch(self): + self.sock = socket.socket() + self.sock.setblocking(False) + try: + self.sock.connect(('xkcd.com', 80)) + except BlockingIOError: + pass + selector.register(self.sock.fileno(), + EVENT_WRITE, + self.connected) + + def connected(self, key, mask): + print('connected!') + # And so on.... +``` + +`fetch`方法开始连接一个套接字,然后注册`connected`回调函数,它会在套接字建立连接后调用。现在我们使用协程把这两步合并: + +```python + def fetch(self): + sock = socket.socket() + sock.setblocking(False) + try: + sock.connect(('xkcd.com', 80)) + except BlockingIOError: + pass + + f = Future() + + def on_connected(): + f.set_result(None) + + selector.register(sock.fileno(), + EVENT_WRITE, + on_connected) + yield f + selector.unregister(sock.fileno()) + print('connected!') +``` + +现在,`fetch`是一个生成器,因为他有一个`yield`语句。我们创建一个未决的future,然后yield它,暂停执行直到套接字连接建立。内函数`on_connected`解决这个future。 + +但是当future被解决,谁来恢复这个生成器?我们需要一个协程驱动器。让我们叫它`task`: + +```python +class Task: + def __init__(self, coro): + self.coro = coro + f = Future() + f.set_result(None) + self.step(f) + + def step(self, future): + try: + next_future = self.coro.send(future.result) + except StopIteration: + return + + next_future.add_done_callback(self.step) + +# Begin fetching http://xkcd.com/353/ +fetcher = Fetcher('/353/') +Task(fetcher.fetch()) + +loop() +``` + +task通过传递一个None值给`fetch`来启动它。`fetch`运行到它yeild一个future,这个future被task捕获作为`next_future`。当套接字连接建立,事件循环运行回调函数`on_connected`,这里future被解决,`step`被调用,生成器恢复运行。 + +## 用`yield from`重构协程 + +一旦套接字连接建立,我们就可以发送HTTP请求,然后读取服务器响应。不再需要哪些分散在各处的回调函数,我们把它们放在同一个生成器函数中: + +```python + def fetch(self): + # ... connection logic from above, then: + sock.send(request.encode('ascii')) + + while True: + f = Future() + + def on_readable(): + f.set_result(sock.recv(4096)) + + selector.register(sock.fileno(), + EVENT_READ, + on_readable) + chunk = yield f + selector.unregister(sock.fileno()) + if chunk: + self.response += chunk + else: + # Done reading. + break +``` + +从套接字中读取所有信息的代码看起来很通用。我们能不把它提取成一个子过程?现在该Python3的`yield from`登场了。它能让一个生成器委托另一个生成器。 + +让我们先回到原来那个简单的生成器: + +```python +>>> def gen_fn(): +... result = yield 1 +... print('result of yield: {}'.format(result)) +... result2 = yield 2 +... print('result of 2nd yield: {}'.format(result2)) +... return 'done' +... +``` + +为了从其他生成器调用这个生成器,我们使用`yield from`: + + +```python +>>> # Generator function: +>>> def caller_fn(): +... gen = gen_fn() +... rv = yield from gen +... print('return value of yield-from: {}' +... .format(rv)) +... +>>> # Make a generator from the +>>> # generator function. +>>> caller = caller_fn() +``` + +这个`caller`的行为的和它委派的生成器表现的完全一致: + +```python +>>> caller.send(None) +1 +>>> caller.gi_frame.f_lasti +15 +>>> caller.send('hello') +result of yield: hello +2 +>>> caller.gi_frame.f_lasti # Hasn't advanced. +15 +>>> caller.send('goodbye') +result of 2nd yield: goodbye +return value of yield-from: done +Traceback (most recent call last): + File "", line 1, in +StopIteration +``` + +注意到`caller`的指令指针保持15不变,就是`yield from`的地方,即使内部的生成器从一个yield语句运行到下一个yield,它始终不变。[^13]从`caller`外部来看,我们无法分辨yield出的值是来自`caller`还是它委派的生成器。而从`gen`内部来看,我们也不能分辨传给它的值是来自`caller`还是`caller`的外面。`yield from`是一个光滑的管道,值通过它进出`gen`,一直到`gen`结束。 + +协程可以用`yield from`把工作委派给子协程,还可以接受子协程的返回值。注意到上面的`caller`打印出"return value of yield-from: done"。当`gen`完成后,它的返回值成为`caller`中`yield from`语句的值。 + +```python + rv = yield from gen +``` + +我们批评过基于回调的异步编程模式,其中最大的不满是关于`stack ripping`:当一个回调抛出异常,它的堆栈回溯通常是毫无用处的。它只显示出事件循环运行了它,而没有说为什么。那么协程怎么样? + +```python +>>> def gen_fn(): +... raise Exception('my error') +>>> caller = caller_fn() +>>> caller.send(None) +Traceback (most recent call last): + File "", line 1, in + File "", line 3, in caller_fn + File "", line 2, in gen_fn +Exception: my error +``` + +这还是比较有用的,当异常抛出时,堆栈回溯显示出`caller_fn`委派了`gen_fn`。令人更欣慰的是,你可以像正常函数一样使用异常处理: + +```python +>>> def gen_fn(): +... yield 1 +... raise Exception('uh oh') +... +>>> def caller_fn(): +... try: +... yield from gen_fn() +... except Exception as exc: +... print('caught {}'.format(exc)) +... +>>> caller = caller_fn() +>>> caller.send(None) +1 +>>> caller.send('hello') +caught uh oh +``` + +所以我们可以像提取子过程一样提取子协程。让我们从fetcher中提取一些有用的子协程。我们先写一个可以读一块数据的协程`read`: + +```python +def read(sock): + f = Future() + + def on_readable(): + f.set_result(sock.recv(4096)) + + selector.register(sock.fileno(), EVENT_READ, on_readable) + chunk = yield f # Read one chunk. + selector.unregister(sock.fileno()) + return chunk +``` + +在`read`的基础上,`read_all`协程读取整个信息: + +```python +def read_all(sock): + response = [] + # Read whole response. + chunk = yield from read(sock) + while chunk: + response.append(chunk) + chunk = yield from read(sock) + + return b''.join(response) +``` + +如果你换个角度看,它们看起来就像在做阻塞I/O的普通函数一样。但是事实上,`read`和`read_all`都是协程。yield from`read`暂停`read_all`直到I/O操作完成。当`read_all`暂停时,事件循环正在做其它的工作等待其他的I/O操作。`read`在下次循环中完成I/O操作时,`read_all`恢复运行。 + +现在,`fetch`可以直接调用`read_all`: + +```python +class Fetcher: + def fetch(self): + # ... connection logic from above, then: + sock.send(request.encode('ascii')) + self.response = yield from read_all(sock) +``` + +神奇的是,Task类不需要做任何改变,它像以前一样驱动`fetch`协程: + +```python +Task(fetcher.fetch()) +loop() +``` + +当`read`yield一个future时,task从`yield from`管道中接受它,就像直接从`fetch`接受一样。当循环解决一个future时,task把它的结果送给`fetch`,通过管道,`read`接受到这个值,这完全就像task直接驱动`read`一样: + +图 + +亲爱的读者,我们已经完成了对asyncio协程探索。我们深入观察了生成器的机制,实现了简单的future和task。我们指出协程是如何利用两个世界的优点:比线程高效,比回调清晰。当然真正的asyncio比我们这个简化版本要复杂的多。真正的框架需要处理zero-copyI/0,公平调度,异常处理和其他大量特性。 + +使用asyncio编写协程代码比你现在看到的要简单的多。在前面的代码中,我们从基本原理去实现协程,所以你看到了回调,task和future,甚至非阻塞套接字和``select``调用。但是当用asyncio编写应用,这些都不会出现在你的代码中。我们承诺过,你可以像这样下载一个网页: + +```python + @asyncio.coroutine + def fetch(self, url): + response = yield from self.session.get(url) + body = yield from response.read() +``` + +对我们的探索还满意么?回到我们原始的任务:使用asyncio写一个网络爬虫。 + +## 使用协程 + +我们从描述爬虫如何工作开始。现在是时候用asynio去实现它了。 + +我们爬虫从获取第一个网页开始,解析出链接并把它们加到队列中。此后它开始傲游整个网站,并发的获取网页。倒是由于客户端和服务端的限制,我们希望有一个最大数目的worker。任何时候一个worker完成一个网页的获取,它应该立即从队列中取出下一个链接。我们会遇到没有事干的时候,所以worker必须能够暂停。一旦又有worker获取一个有很多链接的网页,队列会突增,暂停的worker立马被唤醒。最后,当任务完成后我们的程序必须能退出。 + +假如你的worker是线程,怎样去描述你的算法?我们可以使用Python标准库中的同步队列。每次有新的一项加入,队列增加它的tasks计数器。线程worker完成一个任务后调用`task_done`。主线程阻塞在`Queue.join`,直到tasks计数器与`task_done`调用次数相匹配,然后退出。 + +通过个一asynio队列,协程使用和线程一样的模式来实现。首先我们导入它[^6]: + +```python +try: + from asyncio import JoinableQueue as Queue +except ImportError: + # In Python 3.5, asyncio.JoinableQueue is + # merged into Queue. + from asyncio import Queue +``` + +我们把worker的共享状态收集在一个crawler类中,主要的逻辑写在`crawl`方法中。我们在一个协程中启动`crawl`,运行asyncio的事件循环直到`crawl`完成: + +```python +loop = asyncio.get_event_loop() + +crawler = crawling.Crawler('http://xkcd.com', + max_redirect=10) + +loop.run_until_complete(crawler.crawl()) +``` + +crawler用一个跟URL和最大重定向数来初始化,它把`(URL, max_redirect`)序对放入队列中。(为什么要这样做,敬请期待) + +```python +class Crawler: + def __init__(self, root_url, max_redirect): + self.max_tasks = 10 + self.max_redirect = max_redirect + self.q = Queue() + self.seen_urls = set() + + # aiohttp's ClientSession does connection pooling and + # HTTP keep-alives for us. + self.session = aiohttp.ClientSession(loop=loop) + + # Put (URL, max_redirect) in the queue. + self.q.put((root_url, self.max_redirect)) +``` + +现在队列中未完成的任务数是1。回到我们的主程序,启动事件循环和`crawl`方法: + +```python +loop.run_until_complete(crawler.crawl()) +``` +`crawl`协程唤起workers。它像一个主线程:阻塞在`join`上直到所有任务完成,同时workers在后台运行。 + +```python + @asyncio.coroutine + def crawl(self): + """Run the crawler until all work is done.""" + workers = [asyncio.Task(self.work()) + for _ in range(self.max_tasks)] + + # When all work is done, exit. + yield from self.q.join() + for w in workers: + w.cancel() +``` + +如果worker是线程,可能我们不会一次把它们全部创建出来。为了避免创建线程的昂贵代价,通常一个线程池会按需增长。但是协程很便宜,我们简单的把他们全部创建出来。 + +怎么关闭这个`crawler`很有趣。当`join`完成,worker存活但是被暂停:他们等待更多的URL。所以主协程在退出之前清除它们。否则Python解释器关闭调用所有对象的析构函数,活着的worker叫喊到: + +``` +ERROR:asyncio:Task was destroyed but it is pending! +``` + +`cancel`又是如何工作的呢?生成器还有一个我们还没介绍的特点。你可以从外部抛一个异常给它: + + +```python +>>> gen = gen_fn() +>>> gen.send(None) # Start the generator as usual. +1 +>>> gen.throw(Exception('error')) +Traceback (most recent call last): + File "", line 3, in + File "", line 2, in gen_fn +Exception: error +``` + +生成器被`throw`恢复,但是他现在抛出一个异常。如何生成器没有捕获异常的代码,这个异常被传递到顶层。所以注销一个协程: + +```python + # Method of Task class. + def cancel(self): + self.coro.throw(CancelledError) +``` + +任何时候生成器在`yield from`语句暂停,被恢复并且抛出一个异常。我们在task的`step`方法中处理撤销。 + +现在worker直到他被注销了,所以当它被销毁时,它不再抱怨。 + +一旦`crawl`注销了worker,它就退出。同时事件循环看见这个协程结束了,也就退出l。 + +```python +loop.run_until_complete(crawler.crawl()) +``` + +`crawl`方法包含了所有主协程需要做的事。而worker则完成了从队列中获取URL,获取网页,解析它们得到新的链接。每个worker独立的运行`worker`协程: + +```python + @asyncio.coroutine + def work(self): + while True: + url, max_redirect = yield from self.q.get() + + # Download page and add new links to self.q. + yield from self.fetch(url, max_redirect) + self.q.task_done() +``` + +Python看见这段代码包含`yield from`语句,就把它编译成生成器函数。所以在`crawl`方法中,我们调用了10次`self.work`,但并没有真正执行,它仅仅创建了10个生成器对象并把它们包装成Task对象。task接收生成器yield的future,通过调用send方法,future的结果做为send的参数,来驱动它。由于生成器有自己的栈帧,它们可以独立运行,独立的局部变量和指令指针。 + +worker使用队列来协调, 等待新的URL: + +```python + url, max_redirect = yield from self.q.get() +``` + +队列的`get `方法也是一个协程,它一直暂停到有新的URL进入队列。 + +碰巧,这也是最后crawl停止时,协程暂停的地方。当主协程注销worker时,从协程的角度,`yield from`抛出`CancelledError`结束了它在循环中的最后旅程。 + +worker获取一个网页,解析链接,把新的链接放入队列中,接着调用`task_done`减小计数器。最终一个worker遇到一个没有新链接的网页,并且队列里也没有任务,这次`task_done`的调用使计数器减为0,而`crawl`正阻塞在`join`方法上,现在它就可以结束了。 + +我们承诺过要解释为什么队列中要使用序对,像这样: + +```python +# URL to fetch, and the number of redirects left. +('http://xkcd.com/353', 10) +``` + +新的URL的重定向次数是10。获取一个特别的URL会重定向一个新的位置。我们减小重定向次数,并把新的URL放入队列中。 + +```python +# URL with a trailing slash. Nine redirects left. +('http://xkcd.com/353/', 9) +``` + +我们使用的`aiohttp`默认的会重定向返回最终的结果。但是,我们告诉它不要这样做,爬虫自己处理重定向。所以它可以合并那些目的相同的重定向路径:如果我们已经看到一个URL,说明它已经从其他的地方走过这条路了。 + + +```python + @asyncio.coroutine + def fetch(self, url, max_redirect): + # Handle redirects ourselves. + response = yield from self.session.get( + url, allow_redirects=False) + + try: + if is_redirect(response): + if max_redirect > 0: + next_url = response.headers['location'] + if next_url in self.seen_urls: + # We have been down this path before. + return + + # Remember we have seen this URL. + self.seen_urls.add(next_url) + + # Follow the redirect. One less redirect remains. + self.q.put_nowait((next_url, max_redirect - 1)) + else: + links = yield from self.parse_links(response) + # Python set-logic: + for link in links.difference(self.seen_urls): + self.q.put_nowait((link, self.max_redirect)) + self.seen_urls.update(links) + finally: + # Return connection to pool. + yield from response.release() +``` + +如果这是多进程代码,就有可能遇到讨厌的竞争条件。比如,一个work检查一个链接是否在`seen_urls`中,如果没有它就把这个链接加到队列中并把它放到`seen_urls`中。如果它在这两步操作之间被中断,而另一个work解析到相同的链接,发现它并没有出现在`seen_urls`中就把它加入队列中。这导致同样的链接在队列中出现两次,做了重复的工作和错误的统计。 + +然而,一个协程只在`yield from`是才会被中断。这是协程比多线程少遇到竞争条件的关键。多线程必须获得锁来明确的进入一个临界区,否则它就是可中断的。而Python的协程默认是不会被中断的,只有它yield主动放弃控制权。 + +我们不再需要在用回调方式时的fetcher类了。这个类只是不高效回调的一个变通方法:在等待I/O时,它需要一个存储状态的地方,因为局部变量并不能在函数调用间保留。倒是`fetch`协程可以像普通函数一样用局部变量保存它的状态,所以我们不再需要一个类。 + +当`fetch`完成对服务器回应的处理,它返回到调用它的work。work调用`task_done`,接着从队列中取出一个URL。 + +当`fetch`把新的链接放入队列中,它增加未完成的任务计数器。主协程在等待`q.join`。而当没有新的链接并且这是队列中最后一个URL,work调用`task_done`,任务计数器变为0,主协程从`join`中退出。 + +与work和主协程一起工作的队列代码像这样: + +```python +class Queue: + def __init__(self): + self._join_future = Future() + self._unfinished_tasks = 0 + # ... other initialization ... + + def put_nowait(self, item): + self._unfinished_tasks += 1 + # ... store the item ... + + def task_done(self): + self._unfinished_tasks -= 1 + if self._unfinished_tasks == 0: + self._join_future.set_result(None) + + @asyncio.coroutine + def join(self): + if self._unfinished_tasks > 0: + yield from self._join_future +``` + +主协程`crawl`yield from`join`。所以当最后一个workd把计数器减为0,它告诉`crawl`恢复运行。 + +旅程快要结束了。我们的程序从`crawl`调用开始: + +```python +loop.run_until_complete(self.crawler.crawl()) +``` + +程序如何结束?因为`crawl`是一个生成器函数。调用它返回一个生成器。为了驱动它,asyncio把它包装成一个task: + + +class EventLoop: + def run_until_complete(self, coro): + """Run until the coroutine is done.""" + task = Task(coro) + task.add_done_callback(stop_callback) + try: + self.run_forever() + except StopError: + pass + +class StopError(BaseException): + """Raised to stop the event loop.""" + +def stop_callback(future): + raise StopError +``` + +当这个任务完成,它抛出`StopError`, 事件循环把这个异常当作正常退出的信号。 + +但是,task的`add_done_callbock`和`result`方法又是什么呢?你可能认为task就像一个future,不错,你的直觉是对的。我们必须承认一个向你隐藏的细节,task是future。 + +```python +class Task(Future): + """A coroutine wrapped in a Future.""" +``` + +通常,一个future被别人调用`set_result`。但是task,当协程结束时,它自己解决自己。记得我们解释过当Python生成器返回时,它抛出一个特殊的`StopIteration`异常: + +```python + # Method of class Task. + def step(self, future): + try: + next_future = self.coro.send(future.result) + except CancelledError: + self.cancelled = True + return + except StopIteration as exc: + + # Task resolves itself with coro's return + # value. + self.set_result(exc.value) + return + + next_future.add_done_callback(self.step) +``` + +所以当事件循环调用`task.add_done_callback(stop_callback)`,它就准备被这个task结束。在看一次`run_until_complete`; + +```python + # Method of event loop. + def run_until_complete(self, coro): + task = Task(coro) + task.add_done_callback(stop_callback) + try: + self.run_forever() + except StopError: + pass +``` + +当task捕获`StopIteration`并解决自己,这个回调重循环中抛出`StopError`。循环结束调用栈回到`run_until_complete`。我们的程序结束。 + +## 总结 + +现代的程序越来越多是I/O密集型而不是CPU密集型。对于这样的程序,Python的线程和不合适:全局锁阻止真正的并行计算,并且抢占切换也导致他们更容易出现竞争。异步通常是正确的选择。但是随着基于回调的异步代码增加,它会变得非常混乱。协程是一个更整洁的替代者。它们自然的重构成子过程,有健全的异常处理和栈追溯。 + +如果我们换个角度看`yield from`语句,一个协程看起来像一个传统的线程。甚至我们采用金典的多线程模式编程,不需要重新发明。因此,与回调相比,协程更适合有经验的多线程的编码者。 + +但是当我们打开眼睛关注`yield from`语句,我们能看到协程放弃控制权的标志点。不像多线程,协程展示出我们的代码哪里可以被中断哪里不能。在Glyph Lefkowitz富有启发性的文章"Unyielding"[^4]:"线程让局部推理变得困难,然而局部推理可能是软件开发中最重要的事"。然而,明确的yield,让"通过子过程本身而不是整个系统理解它的行为(应此,正确性)"成为可能。 + +这章写于Python和异步的复兴时期。你刚学到的基于生成器的的协程,在2014年发布在Python 3.4的"asyncio"模块。2015年9月,Python 3.5发布,协程成为语言的一部分。这个原生的协程通过"async def"来声明, 使用"await"而不是"yield from"委托一个协程或者等待Future。 + + +除了这些优点,核心的思想不变。Python新的原生协程与生成器只是在语法上不同,工作原理非常相似。事实上,在Python解释器中它们公用同一个实现方法。Task,Future和事件循环扮演这在asynico中同样的角色。 + +你已经知道asyncio协程是如何工作的了,现在你可以忘记大部分的细节。这些机制隐藏在一个整洁的接口下。但是你对这基本原理的理解能让你在现代异步环境下正确而高效的编写代码。 + +[^4]: [https://glyph.twistedmatrix.com/2014/02/unyielding.html](https://glyph.twistedmatrix.com/2014/02/unyielding.html) + +[^5]: [https://docs.python.org/3/library/queue.html](https://docs.python.org/3/library/queue.html) + +[^6]: [https://docs.python.org/3/library/asyncio-sync.html](https://docs.python.org/3/library/asyncio-sync.html) + +[^7]: For a complex solution to this problem, see [http://www.tornadoweb.org/en/stable/stack_context.html](http://www.tornadoweb.org/en/stable/stack_context.html) + +[^8]: [http://www.kegel.com/c10k.html](http://www.kegel.com/c10k.html) + +[^9]: The actual `asyncio.Queue` implementation uses an `asyncio.Event` in place of the Future shown here. The difference is an Event can be reset, whereas a Future cannot transition from resolved back to pending. + +[^10]: The `@asyncio.coroutine` decorator is not magical. In fact, if it decorates a generator function and the `PYTHONASYNCIODEBUG` environment variable is not set, the decorator does practically nothing. It just sets an attribute, `_is_coroutine`, for the convenience of other parts of the framework. It is possible to use asyncio with bare generators not decorated with `@asyncio.coroutine` at all. + + +[^11]: Jesse listed indications and contraindications for using async in "What Is Async, How Does It Work, And When Should I Use It?", available at pyvideo.org. +[^bayer]: Mike Bayer compared the throughput of asyncio and multithreading for different workloads in his "Asynchronous Python and Databases": http://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/ + + +[^11]: Jesse listed indications and contraindications for using async in ["What Is Async, How Does It Work, And When Should I Use It?":](http://pyvideo.org/video/2565/what-is-async-how-does-it-work-and-when-should). Mike Bayer compared the throughput of asyncio and multithreading for different workloads in ["Asynchronous Python and Databases":](http://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/) + + +[^12]: This future has many deficiencies. For example, once this future is resolved, a coroutine that yields it should resume immediately instead of pausing, but with our code it does not. See asyncio's Future class for a complete implementation. + +[^13]: In fact, this is exactly how "yield from" works in CPython. A function increments its instruction pointer before executing each statement. But after the outer generator executes "yield from", it subtracts 1 from its instruction pointer to keep itself pinned at the "yield from" statement. Then it yields to *its* caller. The cycle repeats until the inner generator throws `StopIteration`, at which point the outer generator finally allows itself to advance to the next instruction. + +[^14]: Python's global interpreter lock prohibits running Python code in parallel in one process anyway. Parallelizing CPU-bound algorithms in Python requires multiple processes, or writing the parallel portions of the code in C. But that is a topic for another day. + +[^15]: Even calls to `send` can block, if the recipient is slow to acknowledge outstanding messages and the system's buffer of outgoing data is full. + + +[^16]: Guido introduced the standard asyncio library, called "Tulip" then, at [PyCon 2013](http://pyvideo.org/video/1667/keynote). + + +[^16]: Guido introduced the standard asyncio library, called "Tulip" then, at PyCon 2013. + + +[^17]: Python 3.5's built-in coroutines are described in [PEP 492](https://www.python.org/dev/peps/pep-0492/), "Coroutines with async and await syntax." + +-------------------------------------- +via: http://aosabook.org/en/500L/pages/a-web-crawler-with-asyncio-coroutines.html + +作者: A. Jesse Jiryu Davis and Guido van Rossum +译者:[qingyunha](https://github.com/qingyunha) +校对: + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 From e3e8a3b3418d36eb494b92d00e125b24e847a701 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 20:23:41 +0800 Subject: [PATCH 10/48] =?UTF-8?q?20160917-1=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ke LibreOffice and OpenOffice one again.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md diff --git a/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md b/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md new file mode 100644 index 0000000000..3aba30f29b --- /dev/null +++ b/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md @@ -0,0 +1,64 @@ +It's time to make LibreOffice and OpenOffice one again +========== + +![](http://tr2.cbsistatic.com/hub/i/2016/09/14/2e91089b-7ebd-4579-bf8f-74c34d1a94ce/e7e9c8dd481d8e068f2934c644788928/openofficedeathhero.jpg) + +Let's talk about OpenOffice. More than likely you've already read, countless times, that Apache OpenOffice is near the end. The last stable iteration was 4.1.2 (released October, 2015) and a recent major security flaw took a month to patch. A lack of coders has brought development to a creeping crawl. And then, the worst possible news hit the ether; the project suggested users switch to MS Office (or LibreOffice). + +For whom the bells tolls? The bell tolls for thee, OpenOffice. + +I'm going to say something that might ruffle a few feathers. Are you ready for it? + +The end of OpenOffice will be a good thing for open source and for users. + +Let me explain. + +### One fork to rule them all + +When LibreOffice was forked from OpenOffice we saw yet another instance of the fork not only improving on the original, but vastly surpassing it. LibreOffice was an instant success. Every Linux distribution that once shipped with OpenOffice migrated to the new kid on the block. LibreOffice burst out of the starting gate and immediately hit its stride. Updates came at an almost breakneck speed and the improvements were plenty and important. + +After a while, OpenOffice became an afterthought for the open source community. This, of course, was exacerbated when Oracle decided to discontinue the project in 2011 and donated the code to the Apache Project. By this point OpenOffice was struggling to move forward and that brings us to now. A burgeoning LibreOffice and a suffering, stuttering OpenOffice. + +But I say there is a light at the end of this rather dim tunnel. + +### Unfork them + +This may sound crazy, but I think it's time LibreOffice and OpenOffice became one again. Yes, I know there are probably political issues and egos at stake, but I believe the two would be better served as one. The benefits of this merger would be many. Off the top of my head: + +- Bring the MS Office filters together: OpenOffice has a strong track record of better importing certain files from MS Office (whereas LibreOffice has been known to be improving, but spotty). +- More developers for LibreOffice: Although OpenOffice wouldn't bring with it a battalion of developers, it would certainly add to the mix. +- End the confusion: Many users assume OpenOffice and LibreOffice are the same thing. Some don't even know that LibreOffice exists. This would end that confusion. +- Combine their numbers: Separate, OpenOffice and LibreOffice have impressive usage numbers. Together, they would be a force. +### A golden opportunity + +The possible loss of OpenOffice could actually wind up being a golden opportunity for open source office suites in general. Why? I would like to suggest something that I believe has been necessary for a while now. If OpenOffice and LibreOffice were to gather their forces, diff their code, and merge, they could then do some much-needed retooling of not just the internal works of the whole, but also of the interface. + +Let's face it, the LibreOffice and (by extension) OpenOffice UIs are both way out of date. When I install LibreOffice 5.2.1.2 the tool bar is an absolute disaster (Figure A). + +### Figure A + +![](http://tr2.cbsistatic.com/hub/i/2016/09/14/cc5250df-48cd-40e3-a083-34250511ffab/c5ac8eb1e2cb12224690a6a3525999f0/openofficea.jpg) + +#### The LibreOffice default toolbar setup. + +As much as I support and respect (and use daily) LibreOffice, it has become all too clear the interface needs a complete overhaul. What we're dealing with now is a throwback to the late 90s/early 2000s and it has to go. When a new user opens up LibreOffice for the first time, they are inundated with buttons, icons, and toolbars. Ubuntu Unity helped this out with the Head up Display (HUD), but that did nothing for other desktops and distributions. Sure, the enlightened user has no problem knowing what to look for and where it is (or to even customize the toolbars to reflect their specific needs), but for a new or average user, that interface is a nightmare. Now would be the perfect time for this change. Bring in the last vestiges of the OpenOffice developers and have them join the fight for an improved interface. With the combination of the additional import filters from OpenOffice and a modern interface, LibreOffice could finally make some serious noise on both the home and business desktops. + +### Will this actually happen? + +This needs to happen. Will it? I have no idea. But even if the powers that be decide the UI isn't in need of retooling (which would be a mistake), bringing OpenOffice into the fold would still be a big step forward. The merging of the two efforts would bring about a stronger focus on development, easier marketing, and far less confusion by the public at large. + +I realize this might seem a bit antithetical to the very heart and spirit of open source, but merging LibreOffice and OpenOffice would combine the strengths of the two constituent pieces and possibly jettison the weaknesses. + +From my perspective, that's a win-win. + +-------------------------------------------------------------------------------- + +via: http://www.techrepublic.com/article/its-time-to-make-libreoffice-and-openoffice-one-again/ + +作者:[Jack Wallen ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.techrepublic.com/search/?a=jack%2Bwallen From e0c3f4b8befaaefe2a2cc7523260099c7214e5d0 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 20:29:59 +0800 Subject: [PATCH 11/48] =?UTF-8?q?20160917-2=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...for building containerized applications.md | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 sources/tech/20160912 8 best practices for building containerized applications.md diff --git a/sources/tech/20160912 8 best practices for building containerized applications.md b/sources/tech/20160912 8 best practices for building containerized applications.md new file mode 100644 index 0000000000..d67ee5c473 --- /dev/null +++ b/sources/tech/20160912 8 best practices for building containerized applications.md @@ -0,0 +1,74 @@ +8 best practices for building containerized applications +==== + +![](https://opensource.com/sites/default/files/styles/image-full-size/public/images/business/containers_2015-2-osdc-lead.png?itok=0yid3gFY) + +Containers are a major trend in deploying applications in both public and private clouds. But what exactly are containers, why have they become a popular deployment mechanism, and how will you need to modify your application to optimize it for a containerized environment? + +### What are containers? + +The technology behind containers has a long history beginning with SELinux in 2000 and Solaris zones in 2005. Today, containers are a combination of several kernel features including SELinux, Linux namespaces, and control groups, providing isolation of end user processes, networking, and filesystem space. + +### Why are they so popular? + +The recent widespread adoption of containers is largely due to the development of standards aimed at making them easier to use, such as the Docker image format and distribution model. This standard calls for immutable images, which are the launching point for a container runtime. Immutable images guarantee that the same image the development team releases is what gets tested and deployed into the production environment. + +The lightweight isolation that containers provide creates a better abstraction for an application component. Components running in containers won't interfere with each other the way they might running directly on a virtual machine. They can be prevented from starving each other of system resources, and unless they are sharing a persistent volume won't block attempting to write to the same files. Containers have helped to standardize practices like logging and metric collection, and they allow for increased multi-tenant density on physical and virtual machines, all of which leads to lower deployment costs. + +### How do you build a container-ready application? + +Changing your application to run inside of a container isn't necessarily a requirement. The major Linux distributions have base images that can run anything that runs on a virtual machine. But the general trend in containerized applications is following a few best practices: + +- 1. Instances are disposable + +Any given instance of your application shouldn't need to be carefully kept running. If one system running a bunch of containers goes down, you want to be able to spin up new containers spread out across other available systems. + +- 2. Retry instead of crashing + +When one service in your application depends on another service, it should not crash when the other service is unreachable. For example, your API service is starting up and detects the database is unreachable. Instead of failing and refusing to start, you design it to retry the connection. While the database connection is down the API can respond with a 503 status code, telling the clients that the service is currently unavailable. This practice should already be followed by applications, but if you are working in a containerized environment where instances are disposable, then the need for it becomes more obvious. + +- 3. Persistent data is special + +Containers are launched based on shared images using a copy-on-write (COW) filesystem. If the processes the container is running choose to write out to files, then those writes will only exist as long as the container exists. When the container is deleted, that layer in the COW filesystem is deleted. Giving a container a mounted filesystem path that will persist beyond the life of the container requires extra configuration, and extra cost for the physical storage. Clearly defining the abstraction for what storage is persisted promotes the idea that instances are disposable. Having the abstraction layer also allows a container orchestration engine to handle the intricacies of mounting and unmounting persistent volumes to the containers that need them. + +- 4. Use stdout not log files + +You may now be thinking, if persistent data is special, then what do I do with log files? The approach the container runtime and orchestration projects have taken is that processes should instead [write to stdout/stderr][1], and have infrastructure for archiving and maintaining [container logs][2]. + +- 5. Secrets (and other configurations) are special too + +You should never hard-code secret data like passwords, keys, and certificates into your images. Secrets are typically not the same when your application is talking to a development service, a test service, or a production service. Most developers do not have access to production secrets, so if secrets are baked into the image then a new image layer will have to be created to override the development secrets. At this point, you are no longer using the same image that was created by your development team and tested by quality engineering (QE), and have lost the benefit of immutable images. Instead, these values should be abstracted away into environment variables or files that are injected at container startup. + +- 6. Don't assume co-location of services + +In an orchestrated container environment you want to allow the orchestrator to send your containers to whatever node is currently the best fit. Best fit could mean a number of things: it could be based on whichever node has the most space right now, the quality of service the container is requesting, whether the container requires persistent volumes, etc. This could easily mean your frontend, API, and database containers all end up on different nodes. While it is possible to force an API container to each node (see [DaemonSets][3] in Kubernetes), this should be reserved for containers that perform tasks like monitoring the nodes themselves. + +- 7. Plan for redundancy / high availability + +Even if you don't have enough load to require an HA setup, you shouldn't write your service in a way that prevents you from running multiple copies of it. This will allow you to use rolling deployments, which make it easy to move load off one node and onto another, or to upgrade from one version of a service to the next without taking any downtime. + +- 8. Implement readiness and liveness checks + +It is common for applications to have startup time before they are able to respond to requests, for example, an API server that needs to populate in-memory data caches. Container orchestration engines need a way to check that your container is ready to serve requests. Providing a readiness check for new containers allows a rolling deployment to keep an old container running until it is no longer needed, preventing downtime. Similarly, a liveness check is a way for the orchestration engine to continue to check that the container is in a healthy state. It is up to the application creator to decide what it means for their container to be healthy, or "live". A container that is no longer live will be killed, and a new container created in its place. + +### Want to find out more? + +I'll be at the Grace Hopper Celebration of Women in Computing in October, come check out my talk: [Containerization of Applications: What, Why, and How][4]. Not headed to GHC this year? Then read on about containers, orchestration, and applications on the [OpenShift][5] and [Kubernetes][6] project sites. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/life/16/9/8-best-practices-building-containerized-applications + +作者:[Jessica Forrester ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/jwforres +[1]: https://docs.docker.com/engine/reference/commandline/logs/ +[2]: http://kubernetes.io/docs/getting-started-guides/logging/ +[3]: http://kubernetes.io/docs/admin/daemons/ +[4]: https://www.eiseverywhere.com/ehome/index.php?eventid=153076&tabid=351462&cid=1350690&sessionid=11443135&sessionchoice=1& +[5]: https://www.openshift.org/ +[6]: http://kubernetes.io/ From 0e23269b58adfd4b558569002db7f396e4173b32 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 20:40:24 +0800 Subject: [PATCH 12/48] =?UTF-8?q?20160917-3=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Monitoring with Shinken on Ubuntu 16.04.md | 364 ++++++++++++++++++ 1 file changed, 364 insertions(+) create mode 100644 sources/tech/20160914 Server Monitoring with Shinken on Ubuntu 16.04.md diff --git a/sources/tech/20160914 Server Monitoring with Shinken on Ubuntu 16.04.md b/sources/tech/20160914 Server Monitoring with Shinken on Ubuntu 16.04.md new file mode 100644 index 0000000000..9580189e1e --- /dev/null +++ b/sources/tech/20160914 Server Monitoring with Shinken on Ubuntu 16.04.md @@ -0,0 +1,364 @@ +Server Monitoring with Shinken on Ubuntu 16.04 +===== + + +Shinken is an open source computer and network monitoring framework written in python and compatible with Nagios. Shinken can be used on all operating systems that can run python applications like Linux, Unix, and Windows. Shinken was written by Jean Gabes as proof of concept for a new Nagios architecture, but it was turned down by the Nagios author and became an independent network and system monitoring tool that stays compatible with Nagios. + +In this tutorial, I will show you how to install Shinken from source and add a Linux host to the monitoring system. I will use Ubuntu 16.04 Xenial Xerus as the operating system for the Shinken server and monitored host. + +### Step 1 - Install Shinken Server + +Shinken is a python framework, we can install it with pip or install it from source. In this step, we will install Shinken from source. + +There are some tasks that have to be completed before we start installing Shinken. + +Install some new python packages and create Linux user with the name "shinken": + +``` +sudo apt-get install python-setuptools python-pip python-pycurl +useradd -m -s /bin/bash shinken +``` + +Download the Shinken source from GitHub repository: + +``` +git clone https://github.com/naparuba/shinken.git +cd shinken/ +``` + +Then install Shinken with the command below: + +``` +git checkout 2.4.3 +python setup.py install +``` + +Next, for better results, we need to install 'python-cherrypy3' from the ubuntu repository: + +``` +sudo apt-get install python-cherrypy3 +``` + +Now Shinken is installed, next we add Shinken to start at boot time and start it: + +``` +update-rc.d shinken defaults +systemctl start shinken +``` + +### Step 2 - Install Shinken Webui2 + +Webui2 is the Shinken web interface available from shinken.io. The easiest way to install Sshinken webui2 is by using the shinken CLI command (which has to be executed as shinken user). + +Login to the shinken user: + +``` +su - shinken +``` + +Initialize the shinken configuration file - The command will create a new configuration .shinken.ini: + +``` +shinken --init +``` + +And install webui2 with this shinken CLI command: + +``` +shinken install webui2 +``` + +![](https://www.howtoforge.com/images/server-monitoring-with-shinken-on-ubuntu-16-04/6.png) + +Webui2 is installed, but we need to install MongoDB and another python package with pip. Run command below as root: + +``` +sudo apt-get install mongodb +pip install pymongo>=3.0.3 requests arrow bottle==0.12.8 +``` + +Next, go to the shinken directory and add the new webui2 module by editing the 'broker-master.cfg' file: + +``` +cd /etc/shinken/brokers/ +vim broker-master.cfg +``` + +Add a new option inside module on line 40: + +``` +modules webui2 +``` + +Save the file and exit the editor. + +Now go to the contacts directory and edit the file 'admin.cfg' for the admin configuration. + +``` +cd /etc/shinken/contacts/ +vim admin.cfg +``` + +Change the values shown below: + +``` +contact_name admin # Username 'admin' +password yourpass # Pass 'mypass' +``` + +Save and exit. + +### Step 3 - Install Nagios-plugins and Shinken Packages + +In this step, we will install Nagios-plugins and some Perl module. Then install additional shinken packages from shinken.io to perform the monitoring. + +Install Nagios-plugins and cpanminus which is required for building and installing the Perl modules: + +``` +sudo apt-get install nagios-plugins* cpanminus +``` + +Install these Perl modules with the cpanm command: + +``` +cpanm Net::SNMP +cpanm Time::HiRes +cpanm DBI +``` + +Now create new link for utils.pm file to shinken the directory and create a new directory for Log_File_Health: + +``` +chmod u+s /usr/lib/nagios/plugins/check_icmp +ln -s /usr/lib/nagios/plugins/utils.pm /var/lib/shinken/libexec/ +mkdir -p /var/log/rhosts/ +touch /var/log/rhosts/remote-hosts.log +``` + +Next, install the shinken packages ssh and linux-snmp for monitoring SSH and SNMP sources from shinken.io: + +``` +su - shinken +shinken install ssh +shinken install linux-snmp +``` + +### Step 4 - Add a New Linux Host/host-one + +We will add a new Linux host that shall be monitored by using an Ubuntu 16.04 server with IP address 192.168.1.121 and hostname 'host-one'. + +Connect to the Linux host-one: + +``` +ssh host1@192.168.1.121 +``` + +Install the snmp and snmpd packages from the Ubuntu repository: + +``` +sudo apt-get install snmp snmpd +``` + +Next, edit the configuration file 'snmpd.conf' with vim: + +``` +vim /etc/snmp/snmpd.conf +``` + +Comment line 15 and uncomment line 17: + +``` +#agentAddress udp:127.0.0.1:161 +agentAddress udp:161,udp6:[::1]:161 +``` + +Comment line 51 and 53, then add new line configuration below: + +``` +#rocommunity mypass default -V systemonly +#rocommunity6 mypass default -V systemonly + +rocommunity mypass +``` + +Save and exit. + +Now start the snmpd service with the systemctl command: + +``` +systemctl start snmpd +``` + +Go to the shinken server and define the new host by creating a new file in the 'hosts' directory. + +``` +cd /etc/shinken/hosts/ +vim host-one.cfg +``` + +Paste configuration below: + +``` +define host{ + use generic-host,linux-snmp,ssh + contact_groups admins + host_name host-one + address 192.168.1.121 + _SNMPCOMMUNITY mypass # SNMP Pass Config on snmpd.conf + } +``` + +Save and exit. + +Edit the SNMP configuration on the Shinken server: + +``` +vim /etc/shinken/resource.d/snmp.cfg +``` + +Change 'public' to 'mypass' - must be the same password that you used in the snmpd configuration file on the client host-one. + +``` +$SNMPCOMMUNITYREAD$=mypass +``` + +Save and exit. + +Now reboot both servers - Shinken server and the monitored Linux host: + +``` +reboot +``` + +The new Linux host has been added successfully to the Shinken server. + +### Step 5 - Access Shinken Webui2 + +Visit the Shinken webui2 on port 7677 (replace the IP in the URL with your IP): + +``` +http://192.168.1.120:7767 +``` + +Log in with user admin and your password (the one that you have set in the admin.cfg configuration file). + +![](https://www.howtoforge.com/images/server-monitoring-with-shinken-on-ubuntu-16-04/1.png) + +Shinken Dashboard in Webui2. + +![](https://www.howtoforge.com/images/server-monitoring-with-shinken-on-ubuntu-16-04/2.png) + +Our 2 servers are monitored with Shinken. + +![](https://www.howtoforge.com/images/server-monitoring-with-shinken-on-ubuntu-16-04/3.png) + +List all services that are monitored with linux-snmp. + +![](https://www.howtoforge.com/images/server-monitoring-with-shinken-on-ubuntu-16-04/4.png) + +Status of all hosts and services. + + +![](https://www.howtoforge.com/images/server-monitoring-with-shinken-on-ubuntu-16-04/5.png) + +### Step 6 - Common Problems with Shinken + +- Problems with the NTP server + +When you get this error with NTP. + +``` +TimeSync - CRITICAL ( NTP CRITICAL: No response from the NTP server) +TimeSync - CRITICAL ( NTP CRITICAL: Offset unknown ) +``` + +To solve this problem, install ntp on all Linux hosts. + +``` +sudo apt-get install ntp ntpdate +``` + +Edit the ntp configuration: + +``` +vim /etc/ntp.conf +``` + +Comment all the pools and replace it with: + +``` +#pool 0.ubuntu.pool.ntp.org iburst +#pool 1.ubuntu.pool.ntp.org iburst +#pool 2.ubuntu.pool.ntp.org iburst +#pool 3.ubuntu.pool.ntp.org iburst + +pool 0.id.pool.ntp.org +pool 1.asia.pool.ntp.org +pool 0.asia.pool.ntp.org +``` + +Next, add a new line inside restrict: + +``` +# Local users may interrogate the ntp server more closely. +restrict 127.0.0.1 +restrict 192.168.1.120 #shinken server IP address +restrict ::1 +NOTE: 192.168.1.120 is the Shinken server IP address. +``` + +Save and exit. + +Start ntp and check the Shinken dashboard: + +``` +ntpd +``` + +- Problem check_netint.pl Not Found + +Download the source from the github repository to the shinken lib directory: + +``` +cd /var/lib/shinken/libexec/ +wget https://raw.githubusercontent.com/Sysnove/shinken-plugins/master/check_netint.pl +chmod +x check_netint.pl +chown shinken:shinken check_netint.pl +``` + +- Problem with NetworkUsage + +There is error message: + +``` +ERROR : Unknown interface eth\d+ +``` + +Check your network interface and edit the linux-snmp template. + +On my Ubuntu server, the network interface is 'enp0s8', not eth0, so I got this error. + +Edit the linux-snmp template packs with vim: + +``` +vim /etc/shinken/packs/linux-snmp/templates.cfg +``` + +Add the network interface to line 24: + +``` +_NET_IFACES eth\d+|em\d+|enp0s8 +``` + +-------------------------------------------------------------------------------- + +via: https://www.howtoforge.com/tutorial/server-monitoring-with-shinken-on-ubuntu-16-04/ + +作者:[Muhammad Arul][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.howtoforge.com/tutorial/server-monitoring-with-shinken-on-ubuntu-16-04/ +Save and exit. From 6eabafb53c63934808ebfc66872ba08a9bf60c0a Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 20:46:03 +0800 Subject: [PATCH 13/48] =?UTF-8?q?20160917-4=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...he Headphone Jack Here Are Our Thoughts.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 sources/talk/20160915 Should Smartphones Do Away with the Headphone Jack Here Are Our Thoughts.md diff --git a/sources/talk/20160915 Should Smartphones Do Away with the Headphone Jack Here Are Our Thoughts.md b/sources/talk/20160915 Should Smartphones Do Away with the Headphone Jack Here Are Our Thoughts.md new file mode 100644 index 0000000000..0bf4b144f9 --- /dev/null +++ b/sources/talk/20160915 Should Smartphones Do Away with the Headphone Jack Here Are Our Thoughts.md @@ -0,0 +1,54 @@ +Should Smartphones Do Away with the Headphone Jack? Here Are Our Thoughts +==== + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/Writers-Opinion-Headphone-Featured.jpg) + +Even though Apple removing the headphone jack from the iPhone 7 has been long-rumored, after the official announcement last week confirming the news, it has still become a hot topic. + +For those not in the know on this latest news, Apple has removed the headphone jack from the phone, and the headphones will now plug into the lightning port. Those that want to still use their existing headphones may, as there is an adapter that ships with the phone along with the lightning headphones. They are also selling a new product: AirPods. These are wireless and are inserted into your ear. The biggest advantage is that by eliminating the jack they were able to make the phone dust and water-resistant. + +Being it’s such a big story right now, we asked our writers, “What are your thoughts on Smartphones doing away with the headphone jack?” + +### Our Opinion + +Derrik believes that “Apple’s way of doing it is a play to push more expensive peripherals that do not comply to an open standard.” He also doesn’t want to have to charge something every five hours, meaning the AirPods. While he understands that the 3.5mm jack is aging, as an “audiophile” he would love a new, open standard, but “proprietary pushes” worry him about device freedom. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/headphone-jacks.jpg) + +Damien doesn’t really even use the headphone jack these days as he has Bluetooth headphones. He hates that wire anyway, so feels “this is a good move.” Yet he also understands Derrik’s point about the wireless headphones running out of battery, leaving him with “nothing to fall back on.” + +Trevor is very upfront in saying he thought it was “dumb” until he heard you couldn’t charge the phone and use the headphones at the same time and realized it was “dumb X 2.” He uses the headphones/headphone jack all the time in a work van without Bluetooth and listens to audio or podcasts. He uses the plug-in style as Bluetooth drains his battery. + +Simon is not a big fan. He hasn’t seen much reasoning past it leaving more room within the device. He figures “it will then come down to whether or not consumers favor wireless headphones, an adapter, and water-resistance over not being locked into AirPods, lightning, or an adapter”. He fears it might be “too early to jump into removing ports” and likes a “one pair fits all” standard. + +James believes that wireless technology is progressive, so he sees it as a good move “especially for Apple in terms of hardware sales.” He happens to use expensive headphones, so personally he’s “yet to be convinced,” noting his Xperia is waterproof and has a jack. + +Jeffry points out that “almost every transition attempt in the tech world always starts with strong opposition from those who won’t benefit from the changes.” He remembers the flak Apple received when they removed the floppy disk drive and decided not to support Flash, and now both are industry standards. He believes everything is evolving for the better, removing the audio jack is “just the first step toward the future,” and Apple is just the one who is “brave enough to lead the way (and make a few bucks in doing so).” + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/Writers-Opinion-Headphone-Headset.jpg) + +Vamsi doesn’t mind the removal of the headphone jack as long as there is a “better solution applicable to all the users that use different headphones and other gadgets.” He doesn’t feel using headphones via a lightning port is a good solution as it renders nearly all other headphones obsolete. Regarding Bluetooth headphones, he just doesn’t want to deal with another gadget. Additionally, he doesn’t get the argument of it being good for water resistance since there are existing devices with headphone jacks that are water resistant. + +Mahesh prefers a phone with a jack because many times he is charging his phone and listening to music simultaneously. He believes we’ll get to see how it affects the public in the next few months. + +Derrik chimed back in to say that by “removing open standard ports and using a proprietary connection too.,” you can be technical and say there are adapters, but Thunderbolt is also closed, and Apple can stop selling those adapters at any time. He also notes that the AirPods won’t be Bluetooth. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/Writers-Opinion-Headphone-AirPods.jpg) + +As for me, I’m always up for two things: New technology and anything Apple. I’ve been using iPhones since a few weeks past the very first model being introduced, yet I haven’t updated since 2012 and the iPhone 5, so I was overdue. I’ll be among the first to get my hands on the iPhone 7. I hate that stupid white wire being in my face, so I just might be opting for AirPods at some point. I am very appreciative of the phone becoming water-resistant. As for charging vs. listening, the charge on new iPhones lasts so long that I don’t expect it to be much of a problem. Even my old iPhone 5 usually lasts about twenty hours on a good day and twelve hours on a bad day. So I don’t expect that to be a problem. + +### Your Opinion + +Our writers have given you a lot to think about. What are your thoughts on Smartphones doing away with the headphone jack? Will you miss it? Is it a deal breaker for you? Or do you relish the upgrade in technology? Will you be trying the iPhone 5 or the AirPods? Let us know in the comments below. + +-------------------------------------------------------------------------------- + +via: https://www.maketecheasier.com/should-smartphones-do-away-with-the-headphone-jack/?utm_medium=feed&utm_source=feedpress.me&utm_campaign=Feed%3A+maketecheasier + +作者:[Laura Tucker][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.maketecheasier.com/author/lauratucker/ From c037ad1f28a2a0b23168821a902facfe02e90212 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 20:50:49 +0800 Subject: [PATCH 14/48] =?UTF-8?q?20160917-5=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...f permissive open source licenses means.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 sources/talk/20160915 What the rise of permissive open source licenses means.md diff --git a/sources/talk/20160915 What the rise of permissive open source licenses means.md b/sources/talk/20160915 What the rise of permissive open source licenses means.md new file mode 100644 index 0000000000..9a17fec1e0 --- /dev/null +++ b/sources/talk/20160915 What the rise of permissive open source licenses means.md @@ -0,0 +1,57 @@ +What the rise of permissive open source licenses means +==== + +Why restrictive licenses such as the GNU GPL are steadily falling out of favor. + +"If you use any open source software, you have to make the rest of your software open source." That's what former Microsoft CEO Steve Ballmer said back in 2001, and while his statement was never true, it must have spread some FUD (fear, uncertainty and doubt) about free software. Probably that was the intention. + +This FUD about open source software is mainly about open source licensing. There are many different licenses, some more restrictive (some people use the term "protective") than others. Restrictive licenses such as the GNU General Public License (GPL) use the concept of copyleft, which grants people the right to freely distribute copies and modified versions of a piece of software as long as the same rights are preserved in derivative works. The GPL (v3) is used by open source projects such as bash and GIMP. There's also the Affero GPL, which provides copyleft to software that is offered over a network (for example as a web service.) + +What this means is that if you take code that is licensed in this way and you modify it by adding some of your own proprietary code, then in some circumstances the whole new body of code, including your code, becomes subject to the restrictive open source license. It was this type of license that Ballmer was probably referring to when he made his statement. + +But permissive licenses are a different animal. The MIT License, for example, lets anyone take open source code and do what they want with it — including modifying and selling it — as long as they provide attribution and don't hold the developer liable. Another popular permissive open source license, the Apache License 2.0, also provides an express grant of patent rights from contributors to users. JQuery, the .NET Core and Rails are licensed using the MIT license, while the Apache 2.0 license is used by software including Android, Apache and Swift. + +Ultimately both license types are intended to make software more useful. Restrictive licenses aim to foster the open source ideals of participation and sharing so everyone gets the maximum benefit from software. And permissive licenses aim to ensure that people can get the maximum benefit from software by allowing them to do what they want with it — even if that means they take the code, modify it and keep it for themselves or even sell the resulting work as proprietary software without contributing anything back. + +Figures compiled by open source license management company Black Duck Software show that the restrictive GPL 2.0 was the most commonly used open source license last year with about 25 percent of the market. The permissive MIT and Apache 2.0 licenses were next with about 18 percent and 16 percent respectively, followed by the GPL 3.0 with about 10 percent. That's almost evenly split at 35 percent restrictive and 34 percent permissive. + +But this snapshot misses the trend. Black Duck's data shows that in the six years from 2009 to 2015 the MIT license's share of the market has gone up 15.7 percent and Apache's share has gone up 12.4 percent. GPL v2 and v3's share during the same period has dropped by a staggering 21.4 percent. In other words there was a significant move away from restrictive licenses and towards permissive ones during that period. + +And the trend is continuing. Black Duck's [latest figures][1] show that MIT is now at 26 percent, GPL v2 21 percent, Apache 2 16 percent, and GPL v3 9 percent. That's 30 percent restrictive, 42 percent permissive — a huge swing from last year’s 35 percent restrictive and 34 percent permissive. Separate [research][2] of the licenses used on GitHub appears to confirm this shift. It shows that MIT is overwhelmingly the most popular license with a 45 percent share, compared to GLP v2 with just 13 percent and Apache with 11 percent. + +![](http://images.techhive.com/images/article/2016/09/open-source-licenses.jpg-100682571-large.idge.jpeg) + +### Driving the trend + +What’s behind this mass move from restrictive to permissive licenses? Do companies fear that if they let restrictive software into the house they will lose control of their proprietary software, as Ballmer warned? In fact, that may well be the case. Google, for example, has [banned Affero GPL software][3] from its operations. + +Jim Farmer, chairman of [Instructional Media + Magic][4], a developer of open source technology for education, believes that many companies avoid restrictive licenses to avoid legal difficulties. "The problem is really about complexity. The more complexity in a license, the more chance there is that someone has a cause of action to bring you to court. Complexity makes litigation more likely," he says. + +He adds that fear of restrictive licenses is being driven by lawyers, many of whom recommend that clients use software that is licensed with the MIT or Apache 2.0 licenses, and who specifically warn against the Affero license. + +This has a knock-on effect with software developers, he says, because if companies avoid software with restrictive licenses then developers have more incentive to license their new software with permissive ones if they want it to get used. + +But Greg Soper, CEO of SalesAgility, the company behind the open source SuiteCRM, believes that the move towards permissive licenses is also being driven by some developers. "Look at an application like Rocket.Chat. The developers could have licensed that with GPL 2.0 or Affero but they chose a permissive license," he says. "That gives the app the widest possible opportunity, because a proprietary vendor can take it and not harm their product or expose it to an open source license. So if a developer wants an application to be used inside a third-party application it makes sense to use a permissive license." + +Soper points out that restrictive licenses are designed to help an open source project succeed by stopping developers from taking other people's code, working on it, and then not sharing the results back with the community. "The Affero license is critical to the health of our product because if people could make a fork that was better than ours and not give the code back that would kill our product," he says. "For Rocket.Chat it's different because if it used Affero then it would pollute companies' IP and so it wouldn't get used. Different licenses have different use cases." + +Michael Meeks, an open source developer who has worked on Gnome, OpenOffice and now LibreOffice, agrees with Jim Farmer that many companies do choose to use software with permissive licenses for fear of legal action. "There are risks with copyleft licenses, but there are also huge benefits. Unfortunately people listen to lawyers, and lawyers talk about risk but they never tell you that something is safe." + +Fifteen years after Ballmer made his inaccurate statement it seems that the FUD it generated it is still having an effect — even if the move from restrictive licenses to permissive ones is not quite the effect he intended. + +-------------------------------------------------------------------------------- + +via: http://www.cio.com/article/3120235/open-source-tools/what-the-rise-of-permissive-open-source-licenses-means.html + +作者:[Paul Rubens ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.cio.com/author/Paul-Rubens/ +[1]: https://www.blackducksoftware.com/top-open-source-licenses +[2]: https://github.com/blog/1964-open-source-license-usage-on-github-com +[3]: http://www.theregister.co.uk/2011/03/31/google_on_open_source_licenses/ +[4]: http://immagic.com/ + From 05c606771846beb89d33acfd36244210fd0d3976 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 20:55:40 +0800 Subject: [PATCH 15/48] =?UTF-8?q?20160917-6=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...from having open source program offices.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sources/tech/20160913 4 big ways companies benefit from having open source program offices.md diff --git a/sources/tech/20160913 4 big ways companies benefit from having open source program offices.md b/sources/tech/20160913 4 big ways companies benefit from having open source program offices.md new file mode 100644 index 0000000000..674ddfdb85 --- /dev/null +++ b/sources/tech/20160913 4 big ways companies benefit from having open source program offices.md @@ -0,0 +1,50 @@ +4 big ways companies benefit from having open source program offices +==== + +![](https://opensource.com/sites/default/files/styles/image-full-size/public/images/business/BUSINESS_creativity.png?itok=x2HTRKVW) + +In the first article in my series on open source program offices, I took a deep dive into [what an open source program office is and why your company might need one][1]. Next I looked at [how Google created a new kind of open source program office][2]. In this article, I'll explain a few benefits of having an open source program office. + +At first glance, one big reason why a company not in the business of software development might more enthusiastically embrace an open source program office is because they have less to lose. After all, they're not gambling with software products that are directly tied to revenue. Facebook, for example, can easily unleash a distributed key-value datastore as an open source project because they don't sell a product called "enterprise key-value datastore." That answers the question of risk, but it still doesn't answer the question of what they gain from contributing to the open source ecosystem. Let's look at a few potential reasons and then tackle each. You'll notice a lot of overlap with vendor open source program offices, but some of the motivations are slightly different. + +### Recruiting + +Recruiting is perhaps the easiest way to sell an open source program office to upper management. Show them the costs associated with recruiting, as well as the return on investment, and then explain how developing relationships with talented engineers results in a pipeline of talented developers who are actually familiar with your technology and excited to help work on it. We don't really need to go in more depth here—it's self-explanatory, right? + +### Technology influence + +Once upon a time, companies that didn't specialize in selling software were powerless to influence development cycles of their software vendors directly, especially if they were not a large customer. Open source completely changed that dynamic and brought users onto a more level playing field with vendors. With the rise of open source development, anyone could push technology into a chosen direction, assuming they were willing to invest the time and resources. But these companies learned that simply investing in developer time, although fruitful, could be even more useful if tied to an overarching strategic effort&mdash. Think bug fixes vs. software architects—lots of companies push bug fixes to upstream open source projects, but some of these same companies began to learn that coordinating a sustained effort with a deeper commitment paid off with faster feature development, which could be good for business. With the open source program office model, companies have staffers who can sniff out strategically important open source communities in which they then invest developer resources. + +With rapidly growing companies such as Google and Facebook, providing leadership in existing open source projects still proved insufficient for their expanding businesses. Facing the challenges of intense growth and building out hyperscale systems, many of the largest companies had built highly customized stacks of software for internal use only. What if they could convince others to collaborate on some of these infrastructure projects? Thus, while they maintained investments in areas such as the Linux kernel, Apache, and other existing projects, they also began to release their own large projects. Facebook released Cassandra, Twitter created Mesos, and eventually Google created the Kubernetes project. These projects have become major platforms for industry innovation, proving to be spectacular successes for the companies involved. (Note that Facebook stopped using Cassandra internally after it needed to create a new software project to solve the problem at a larger scale. However, by that time Cassandra had already become popular and DataStax had formed to take on development). Each of these projects have spawned entire ecosystems of developers, related projects, and end users that serve to accelerate growth and development. + +This would not have been possible without coordination between an open source program office and strategic company initiatives. Without that effort, each of the companies mentioned would still be trying to solve these problems individually—and more slowly. Not only have these projects helped solve business problems internally, they also helped establish the companies that created them as industry heavyweights. Sure, Google has been an industry titan for a few years now, but the growth of Kubernetes ensures both better software, and a direct say in the future direction of container technologies, even more than it already had. These companies are still known for their hyperscale infrastructure and for simply being large Silicon Valley stalwarts. Lesser known, but possibly even more important, is their new relevance as technology producers. Open source program offices guide these efforts and maximize their impact, through technology recommendations and relationships with influential developers, not to mention deep expertise in community governance and people management. + +### Marketing power + +Going hand-in-hand with technology influence is how each company talks about its open source efforts. By honing the messages around these projects and communities, an open source program office is able to deliver maximum impact through targeted marketing campaigns. Marketing has long been a dirty word in open source circles, because everyone has had a bad experience with corporate marketing. In the case of open source communities, marketing takes on a vastly different form from a traditional approach and involves amplifying what is already happening in the communities of strategic importance. Thus, an open source program office probably won't create whiz-bang slides about a project that hasn't even released any code yet, but they'll talk about the software they've created and other initiatives they've participated in. Basically, no vaporware here. + +Think of the first efforts made by Google's open source program office. They didn't simply contribute code to the Linux kernel and other projects—they talked about it a lot, often in keynotes at open source conferences. They didn't just give money to students who write open source code—they created a global program, the Google Summer of Code, that became a cultural touchstone of open source development. This marketing effort cemented Google's status as a major open source developer long before Kubernetes was even developed. As a result, Google wielded major influence during the creation of the GPLv3 license, and company speakers and open source program office representatives became staples at tech events. The open source program office is the entity best situated to coordinate these efforts and deliver real value for the parent company. + +### Improve internal processes + +Improving internal processes may not sound like a big benefit, but overcoming chaotic internal processes is a challenge for every open source program office, whether software vendor or company-driven. Whereas a software vendor must make sure that their processes don't step on products they release (for example, unintentionally open sourcing proprietary software), a user is more concerned with infringement of intellectual property (IP) law: patents, copyrights, and trademarks. No one wants to get sued simply for releasing software. Without an active open source program office to manage and coordinate licensing and other legal questions, large companies face great difficulty in arriving at a consensus around open source processes and governance. Why is this important? If different groups release software under incompatible licenses, not only will this prove to be an embarrassment, it also will provide a significant obstacle to achieving one of the most basic goals—improved collaboration. + +Combined with the fact that many of these companies are still growing incredibly quickly, an inability to establish basic rules around process will prove unwieldy sooner than expected. I've seen large spreadsheets with a matrix of approved and unapproved licenses as well as guidelines for how to (and how not to) create open source communities while complying with legal restrictions. The key is to have something that developers can refer to when they need to make decisions, without incurring the legal overhead of a massive, work-slowing IP review every time a developer wants to contribute to an open source community. + +Having an active open source program office that maintains rules over license compliance and source contribution, as well as establishing training programs for engineers, helps to avoid potential legal pitfalls and costly lawsuits. After all, what good is better collaboration on open source projects if the company loses real money because someone didn't read the license? The good news is that companies have less to worry about with respect to proprietary IP when compared to software vendors. The bad news is that their legal questions are no less complex, especially when they run directly into the legal headwinds of a software vendor. + +How has your organization benefited from having an open source program office? Let me know about it in the comments. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/business/16/9/4-big-ways-companies-benefit-having-open-source-program-offices + +作者:[John Mark Walker][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/johnmark +[1]: https://opensource.com/business/16/5/whats-open-source-program-office +[2]: https://opensource.com/business/16/8/google-open-source-program-office From 0a9a592e894f13a14a2ec850100577a1d7ac8494 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:01:18 +0800 Subject: [PATCH 16/48] =?UTF-8?q?20160917-7=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...s a Slick Desktop REST Client for Linux.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 sources/tech/20160915 Insomnia 3.0 Is a Slick Desktop REST Client for Linux.md diff --git a/sources/tech/20160915 Insomnia 3.0 Is a Slick Desktop REST Client for Linux.md b/sources/tech/20160915 Insomnia 3.0 Is a Slick Desktop REST Client for Linux.md new file mode 100644 index 0000000000..f3a0fc717e --- /dev/null +++ b/sources/tech/20160915 Insomnia 3.0 Is a Slick Desktop REST Client for Linux.md @@ -0,0 +1,51 @@ +Insomnia 3.0 Is a Slick Desktop REST Client for Linux +===== + +![](http://www.omgubuntu.co.uk/wp-content/uploads/2016/09/insomnia-app-screenshot.png) + +Looking for a free REST client for the Linux desktop? Don’t lose sleep: get [Insomnia][1]. + +The app is cross-platform and works on Linux, macOS and Windows. Its developer, Gregory Schier, told us that he created the app “to help developers communicate with [REST APIs][2].” + +He also told that Insomnia already has around 10,000 active users — 9% of which are on Linux. + +“So far, the feedback from Linux users has been very positive because similar applications (not nice ones anyway) aren’t usually available for Linux.” + +Insomnia aims to ‘speed up your API testing workflow’, by letting you organise, run and debug HTTP requests through a cleanly designed interface. + +The app also includes advanced features like cookie management, global environments, SSL validation, and code snippet generation. + +As I am not a developer I can’t evaluate this app first-hand, nor tell you why it rocks or highlight any major feature deficiencies. + +But I thought I’d bring the app to your attention and let you decide for yourself. If you’ve been hunting for a slickly designed GUI alternative to command-line tools like HTTPie, it might be well worth giving it a whirl. + +### Download Insomnia 3.0 for Linux + +Insomnia 3.0 (not to be confused with Insomnia v2.0 which is only available on Chrome) is available to download for Windows, macOS and Linux. + +[Download Insomnia 3.0][4] + +An installer is available for Ubuntu 14.04 LTS and up, as is a cross-distro AppImage: + +[Download Insomnia 3.0 (.AppImage)][5] + +If you want to keep pace with development of the app you can follow [Insomnia on Twitter][6]. + + +-------------------------------------------------------------------------------- + +via: http://www.omgubuntu.co.uk/2016/09/insomnia-3-is-free-rest-client-for-linux?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+d0od+%28OMG%21+Ubuntu%21%29 + +作者:[JOEY-ELIJAH SNEDDON ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://plus.google.com/117485690627814051450/?rel=author +[1]: http://insomnia.rest/ +[2]: https://en.wikipedia.org/wiki/Representational_state_transfer +[3]: https://github.com/jkbrzt/httpie +[4]: https://insomnia.rest/download/ +[5]: https://builds.insomnia.rest/downloads/linux/latest +[6]: https://twitter.com/GetInsomnia From fe9c6eec5c36341051d3078f5532966955383c10 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:05:14 +0800 Subject: [PATCH 17/48] =?UTF-8?q?20160917-7=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...peed Up LibreOffice with 4 Simple Steps.md | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 sources/tech/20160914 How to Speed Up LibreOffice with 4 Simple Steps.md diff --git a/sources/tech/20160914 How to Speed Up LibreOffice with 4 Simple Steps.md b/sources/tech/20160914 How to Speed Up LibreOffice with 4 Simple Steps.md new file mode 100644 index 0000000000..a3dc030f3d --- /dev/null +++ b/sources/tech/20160914 How to Speed Up LibreOffice with 4 Simple Steps.md @@ -0,0 +1,90 @@ +How to Speed Up LibreOffice with 4 Simple Steps +==== + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/speed-up-libreoffice-featured-2.jpg) + +For many fans and supporters of Open Source software, LibreOffice is the best alternative to Microsoft Office, and it has definitely seen huge improvements over the last few releases. However, the initial startup experience still leaves a lot to be desired. There are ways to improve launch time and overall performance of LibreOffice. + +I will go over some practical steps that you can take to improve the load time and responsiveness of LibreOffice in the paragraphs below. + +### 1. Increase Memory Per Object and Image Cache + +This will help the program load faster by allocating more memory resources to the image cache and objects. + +1. Launch LibreOffice Writer (or Calc) + +2. Navigate to “Tools -> Options” in the menubar or use the keyboard shortcut “Alt + F12.” + +3. Click “Memory” under LibreOffice and increase “Use for LibreOffice” to 128MB. + +4. Also increase “Memory per object” to 20Mb. + +5. Click “Ok” to save your changes. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/speed-up-libreoffice-step-1.png) + +Note: You can set the numbers higher or lower than the suggested values depending on how powerful your machine is. It is best to experiment and see which value gives you the optimum performance. + +### 2. Enable LibreOffice QuickStarter + +If you have a generous amount of RAM on your machine, say 4GB and above, you can enable the “Systray Quickstarter” option to keep part of LibreOffice in memory for quicker response with opening new documents. + +You will definitely see improved performance in opening new documents after enabling this option. + +1. Open the options dialog by navigating to “Tools -> Options.” + +2. In the sidebar under “LibreOffice”, select “Memory.” + +3. Tick the “Enable Systray Quickstarter” checkbox. + +4. Click “OK” to save the changes. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/speed-up-libreoffice-2.png) + +Once this option is enabled, you will see the LibreOffice icon in your system tray with options to open any type of document. + +### 3. Disable Java Runtime + +Another easy way to speed up the launch time and responsiveness of LibreOffice is to disable Java. + +1. Open the Options dialog using “Alt + F12.” + +2. In the sidebar, select “LibreOffice,” then “Advanced.” + +3. Uncheck the “Use Java runtime environment” option. + +4. Click “OK” to close the dialog. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/speed-up-libreoffice-3.png) + +If all you use is Writer and Calc, disabling Java will not stop you from working with your files as normal. But to use LibreOffice Base and some other special features, you may need to re-enable it again. In that case, you will get a popup asking if you wish to turn it back on. + +### 4. Reduce Number of Undo Steps + +By default, LibreOffice allows you to undo up to 100 changes to a document. Most users do not need anywhere near that, so holding that many steps in memory is largely a waste of resources. + +I recommend that you reduce this number to 20 to free up memory for other things, but feel free to customise this part to suit your needs. + +1. Open the options dialog by navigating to “Tools -> Options.” + +2. In the sidebar under “LibreOffice,” select “Memory.” + +3. Under “Undo” and change the number of steps to your preferred value. + +4. Click “OK” to save the changes. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/speed-up-libreoffice-5.png) + +If the tips provided helped you speed up the launch time of your LibreOffice Suite, let us know in the comments. Also, please share any other tips you may know for others to benefit as well. + +-------------------------------------------------------------------------------- + +via: https://www.maketecheasier.com/speed-up-libreoffice/?utm_medium=feed&utm_source=feedpress.me&utm_campaign=Feed%3A+maketecheasier + +作者:[Ayo Isaiah][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.maketecheasier.com/author/ayoisaiah/ From f30802a6ad382c1bb81fd6b086903246a18b3626 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:08:00 +0800 Subject: [PATCH 18/48] =?UTF-8?q?20160917-8=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Linux Server Distros Worth Checking Out.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sources/tech/20160912 Five Linux Server Distros Worth Checking Out.md diff --git a/sources/tech/20160912 Five Linux Server Distros Worth Checking Out.md b/sources/tech/20160912 Five Linux Server Distros Worth Checking Out.md new file mode 100644 index 0000000000..68816e2b8e --- /dev/null +++ b/sources/tech/20160912 Five Linux Server Distros Worth Checking Out.md @@ -0,0 +1,44 @@ +Five Linux Server Distros Worth Checking Out +==== + +>Pretty much any of the nearly 300 Linux distributions you'll find listed on Distrowatch can be made to work as servers. Here are those that stand out above the rest. + +![](http://windowsitpro.com/site-files/windowsitpro.com/files/imagecache/large_img/uploads/2016/09/cloudservers.jpg) + +Pretty much any of the nearly 300 Linux distributions you'll find listed on Distrowatch can be made to work as servers. Since Linux's earliest days, users have been provisioning "all purpose" distributions such as Slackware, Debian and Gentoo to do heavy lifting as servers for home and business. That may be fine for the hobbyist, but its a lot of unnecessary work for the professional. + +From the beginning, however, there have been distributions with no other purpose but to serve files and applications, help workstations share common peripherals, serve-up web pages and all the other things we ask servers to do, whether in the cloud, in a data center or on a shelf in a utility closet. + +Here's a look at four of the most used Linux server distros, as well as one distro that might fit the bill for smaller businesses. + +**Red Hat Enterprise Linux**: Perhaps the best known of Linux server distros, RHEL has a reputation for being a solid distribution ready for the most demanding mission critical tasks -- like running the New York Stock Exchange for instance. It's also backed by Red Hat's best-of-breed support. + +The downside? While Red Hat is known for offering customer service and support that's second to none, its support subscriptions aren't cheap. Some might point out, however, that you get what you pay for. Cheaper third party support for RHEL is available, but you might want to do some research before going that route. + +**CentOS**: Anyone who likes RHEL but would like to avoid shoveling money to Red Hat for support should take a look at CentOS, which is basically an RHEL fork. Although it's been around since 2004, in 2014 it became officially sponsored by Red Hat, which now employs most of the project's developers. This means that security patches and bug fixes are made available to CentOS soon after they're pushed to Red Hat. + +If you're going to deploy CentOS, you'll need people with Linux skills on staff, because as far as technical support goes, you're mainly on your own. The good news is that the CentOS community offers excellent resources, such as mailing lists, web forums, and chat rooms, so help is available to those who search. + +**Ubuntu Server**: When Canonical announced many years back that it was coming out with a server edition of Ubuntu, you could hear the snickers. Laughter turned into amazement rather quickly, however, as Ubuntu Server rapidly took hold. This was partly due to the DNA it shares as a derivative of Debian, which has long been a favorite base for Linux servers. Ubuntu filled a gap by adding affordable technical support, superior hardware support, developer tools and lots of polish. + +How popular is Ubuntu Server? Recent figures show it being the most deployed operating system both on OpenStack and on the Amazon Elastic Compute Cloud, where it outpaces second place Amazon Linux Amazon Machine Image by a mile and leaves third place Windows in the virtual dust. Another study shows it as the most used Linux web server. + +**SUSE Linux Enterprise Server**: This German distro has a large base of users in Europe, and was a top server distro on this side of the Atlantic until PR issues arose after it was bought by Novell in the early part of the century. With those days long behind it, SUSE has been gaining ground in the US, and its use will probably accelerate now that HPE is naming it as its preferred Linux partner. + +SUSE Linux Enterprise Server, or SLES, is stable and easy to maintain, which you'd expect for a distro that's been around for nearly as long as Linux itself. Affordable 24/7 "rapid-response" technical support is available, making it suitable for mission critical deployments. + +**ClearOS**: Based on RHEL, ClearOS is included here because it's simple enough for anyone, even most non-techies, to configure. Targeted at small to medium sized businesses, it can also be used as an entertainment server by home users. Using a web-based administration interface for ease-of-use, it's built with the premise in mind that "building your IT infrastructure should be as simple as downloading apps on your smart phone." + +The latest release, version 7.2, includes capabilities that might not be expected from a "lightweight" offering, such as VM support which includes Microsoft Hyper-V, support for the XFS and BTRFS file systems, as well as support for LVM caching and IPv6. Available in a free version or in an inexpensive "professional" version that comes with a variety of support options. + +-------------------------------------------------------------------------------- + +via: http://windowsitpro.com/industry/five-linux-server-distros-worth-checking-out + +作者:[Christine Hall][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://windowsitpro.com/industry/five-linux-server-distros-worth-checking-out From f769e2ccf0a50e7610d435ebbe00868db25edbe3 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:17:28 +0800 Subject: [PATCH 19/48] =?UTF-8?q?20160917-9=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... NOW YOU CAN GOOGLE FROM LINUX TERMINAL.md | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 sources/tech/20160915 GOOGLER: NOW YOU CAN GOOGLE FROM LINUX TERMINAL.md diff --git a/sources/tech/20160915 GOOGLER: NOW YOU CAN GOOGLE FROM LINUX TERMINAL.md b/sources/tech/20160915 GOOGLER: NOW YOU CAN GOOGLE FROM LINUX TERMINAL.md new file mode 100644 index 0000000000..29220291bf --- /dev/null +++ b/sources/tech/20160915 GOOGLER: NOW YOU CAN GOOGLE FROM LINUX TERMINAL.md @@ -0,0 +1,129 @@ +GOOGLER: NOW YOU CAN GOOGLE FROM LINUX TERMINAL! +==== + +![](https://itsfoss.com/wp-content/uploads/2016/09/google-from-linux-terminal.jpg) + +A quick question: What do you do every day? Of course, a lot of things. But I can tell one thing, you search on Google almost every day (if not every day). Am I right? + +Now, if you are a Linux user (which I’m guessing you are) here’s another question: wouldn’t it be nice if you can Google without even leaving the terminal? Without even firing up a Browser window? + +If you are a *nix enthusiast and also one of those people who just love the view of the terminal, I know your answer is – Yes. And I think, the rest of you will also like the nifty little tool I’m going to introduce today. It’s called Googler! + +### GOOGLER: GOOGLE IN YOUR LINUX TERMINAL + +Googler is a straightforward command-line utility for Google-ing right from your terminal window. Googler mainly supports three types of Google Searches: + +- Google Search: Simple Google searching, equivalent to searching on Google homepage. +- Google News Search: Google searching for News, equivalent to searching on Google News. +- Google Site Search: Google searching for results from a specific site. + +Googler shows the search results with the title, URL and page excerpt. The search results can be opened directly in the browser with only a couple of keystrokes. + +![](https://itsfoss.com/wp-content/uploads/2016/09/googler-1.png) + +### INSTALLATION ON UBUNTU + +Let’s go through the installation process first. + +At first make sure you have python version 3.3 or later using this command: + +``` +python3 --version +``` + +If not, upgrade it. Googler requires python 3.3+ for running. + +Though Googler is yet not available through package repository on Ubuntu, we can easily install it from the GitHub repository. All we have to do is run the following commands: + +``` +cd /tmp +git clone https://github.com/jarun/googler.git +cd googler +sudo make install +cd auto-completion/bash/ +sudo cp googler-completion.bash /etc/bash_completion.d/ +``` + +And that’s it. Googler is installed along with command autocompletion feature. + +### FEATURES & BASIC USAGE + +If we go through all its features, Googler is actually quite powerful a tool. Some of the main features are: + +Interactive Interface: Run the following command in terminal: + +``` +googler +``` + +The interactive interface will be opened. The developer of Googler, Arun [Prakash Jana][1] calls it the omniprompt. You can enter ? for available commands on omniprompt. + +![](https://itsfoss.com/wp-content/uploads/2016/09/googler-2.png) + +From the omniprompt, enter any search phrases to initiate the search. You can then enter n or p to navigate next or previous page of search results. + +To open any search result in a browser window, just enter the index number of that result. Or you can open the search page itself by entering o . + +- News Search: If you want to search News, start googler with the N optional argument: + +``` +googler -N +``` + +The subsequent omniprompt will fetch results from Google News. + +- Site Search: If you want to search pages from a specific site, run googler with w {domain} argument: + +``` +googler -w itsfoss.com +``` + +The subsequent omniprompt with fetch results only from It’s FOSS blog! + +- Manual Page: Run the following command for Googler manual page equipped with various examples: + +``` +man googler +``` + +- Google country/domain specific search: + +``` +googler -c in "hello world" +``` + +The above example command will open search results from Google’s Indian domain (in for India). + +- Filter search results by duration and language preference. +- Google search keywords support, such as: site:example.com or filetype:pdf etc. +- HTTPS proxy support. +- Shell commands autocomplete. +- Disable automatic spelling correction. + +There are much more. You can twist Googler to suit your needs. + +Googler can also be integrated with a text-based browser ( like – [elinks][2], [links][3], [lynx][4], w3m etc.), so that you wouldn’t even need to leave the terminal for browsing web pages. The instructions can be found on the [GitHub project page of Googler][5]. + +If you want a graphical demonstration of Googler’s various features, feel free to check the terminal recording attached to the GitHub project page : [jarun/googler v2.7 quick demo][6]. + +### THOUGHTS ON GOOGLER? + +Though Googler might not feel necessary or desired to everybody, for someone who doesn’t want to open the browser just for searching on google or simply want to spend as much as time possible on the terminal window, it is a great tool indeed. What do you think? + +-------------------------------------------------------------------------------- + +via: http://linoxide.com/firewall/pfsense-setup-basic-configuration/ + +作者:[Munif Tanjim][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://itsfoss.com/author/munif/ +[1]: https://github.com/jarun +[2]: http://elinks.or.cz/ +[3]: http://links.twibright.com/ +[4]: http://lynx.browser.org/ +[5]: https://github.com/jarun/googler#faq +[6]: https://asciinema.org/a/85019 From 62c22d62a2625f8b1e83d8d250be92929879c90f Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:24:50 +0800 Subject: [PATCH 20/48] =?UTF-8?q?20160917-10=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20160506 Setup honeypot in Kali Linux.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 sources/tech/20160506 Setup honeypot in Kali Linux.md diff --git a/sources/tech/20160506 Setup honeypot in Kali Linux.md b/sources/tech/20160506 Setup honeypot in Kali Linux.md new file mode 100644 index 0000000000..cb85992389 --- /dev/null +++ b/sources/tech/20160506 Setup honeypot in Kali Linux.md @@ -0,0 +1,84 @@ +Setup honeypot in Kali Linux +==== + +The Pentbox is a safety kit containing various tools for streamlining PenTest conducting a job easily. It is programmed in Ruby and oriented to GNU / Linux, with support for Windows, MacOS and every systems where Ruby is installed. In this small article we will explain how to set up a honeypot in Kali Linux. If you don’t know what is a honeypot, “a honeypot is a computer security mechanism set to detect, deflect, or, in some manner, counteract attempts at unauthorized use of information systems.” + +### Download Pentbox: + +Simply type in the following command in your terminal to download pentbox-1.8. + +``` +root@kali:~# wget http://downloads.sourceforge.net/project/pentbox18realised/pentbox-1.8.tar.gz +``` + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-1.jpg) + +### Uncompress pentbox files + +Decompressing the file with the following command: + +``` +root@kali:~# tar -zxvf pentbox-1.8.tar.gz +``` + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-2.jpg) + +### Run pentbox ruby script + +Change directory into pentbox folder + +``` +root@kali:~# cd pentbox-1.8/ +``` + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-3.jpg) + +Run pentbox using the following command + +``` +root@kali:~# ./pentbox.rb +``` + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-4.jpg) + +### Setup a honeypot + +Use option 2 (Network Tools) and then option 3 (Honeypot). + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-5.jpg) + +Finally for first test, choose option 1 (Fast Auto Configuration) + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-6.jpg) + +This opens up a honeypot in port 80. Simply open browser and browse to http://192.168.160.128 (where 192.168.160.128 is your IP Address. You should see an Access denied error. + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-7.jpg) + +and in the terminal you should see “HONEYPOT ACTIVATED ON PORT 80” followed by “INTRUSION ATTEMPT DETECTED”. + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-8.jpg) + +Now, if you do the same steps but this time select Option 2 (Manual Configuration), you should see more extra options + +![](https://www.blackmoreops.com/wp-content/uploads/2016/05/Set-up-a-honeypot-in-Kali-Linux-blackMORE-Ops-9.jpg) + +Do the same steps but select port 22 this time (SSH Port). Then do a port forwarding in your home router to forward port external port 22 to this machines’ port 22. Alternatively, set it up in a VPS in your cloud server. + +You’d be amazed how many bots out there scanning port SSH continuously. You know what you do then? You try to hack them back for the lulz! + +Here’s a video of setting up honeypot if video is your thing: + + + +-------------------------------------------------------------------------------- + +via: https://www.blackmoreops.com/2016/05/06/setup-honeypot-in-kali-linux/ + +作者:[blackmoreops.com][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: blackmoreops.com From 64ced29ce53b49c80454936526a9c96c17ced53a Mon Sep 17 00:00:00 2001 From: Chao-zhi Liu Date: Sat, 17 Sep 2016 21:29:19 +0800 Subject: [PATCH 21/48] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90=2020?= =?UTF-8?q?16-9-17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...And Is ‘One Fits All’ Linux Packages Useful.md | 112 ------------------ ...And Is ‘One Fits All’ Linux Packages Useful.md | 111 +++++++++++++++++ 2 files changed, 111 insertions(+), 112 deletions(-) delete mode 100644 sources/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md create mode 100644 translated/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md diff --git a/sources/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md b/sources/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md deleted file mode 100644 index b1c955bf5b..0000000000 --- a/sources/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md +++ /dev/null @@ -1,112 +0,0 @@ -Translating by Chao-zhi - -Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful? -================================================================================= - -![](http://www.iwillfolo.com/wordpress/wp-content/uploads/2016/06/Flatpak-and-Snap-Packages.jpg) - -An in-depth look into the new generation of packages starting to permeate the Linux ecosystem. - - -Lately we’ve been hearing more and more about Ubuntu’s Snap packages and Flatpak (formerly referred to as xdg-app) created by Red Hat’s employee Alexander Larsson. - -These 2 types of next generation packages are in essence having the same goal and characteristics which are: being standalone packages that doesn’t rely on 3rd-party system libraries in order to function. - -This new technology direction which Linux seems to be headed is automatically giving rise to questions such as, what are the advantages / disadvantages of standalone packages? does this lead us to a better Linux overall? what are the motives behind it? - -To answer these questions and more, let us explore the things we know about Snap and Flatpak so far. - -### The Motive - -According to both [Flatpak][1] and [Snap][2] statements, the main motive behind them is to be able to bring one and the same version of application to run across multiple Linux distributions. - ->“From the very start its primary goal has been to allow the same application to run across a myriad of Linux distributions and operating systems.” Flatpak - ->“… ‘snap’ universal Linux package format, enabling a single binary package to work perfectly and securely on any Linux desktop, server, cloud or device.” Snap - -To be more specific, the guys behind Snap and Flatpak (S&F) believe that there’s a barrier of fragmentation on the Linux platform. - -A barrier which holds back the platform advancement by burdening developers with more, perhaps unnecessary, work to get their software run on the many distributions out there. - -Therefore, as leading Linux distributions (Ubuntu & Red Hat), they wish to eliminate the barrier and strengthen the platform in general. - -But what are the more personal gains which motivate the development of S&F? - -#### Personal Gains? - -Although not officially stated anywhere, it may be assumed that by leading the efforts of creating a unified package that could potentially be adopted by the vast majority of Linux distros (if not all of them), the captains of these projects could assume a key position in determining where the Linux ship sails. - -### The Advantages - -The benefits of standalone packages are diverse and can depend on different factors. - -Basically however, these factors can be categorized under 2 distinct criteria: - -#### User Perspective - -+ From a Linux user point of view, Snap and Flatpak both bring the possibility of installing any package (software / app) on any distribution the user is using. - -That is, for instance, if you’re using a not so popular distribution which has only a scarce supply of packages available in their repo, due to workforce limitations probably, you’ll now be able to easily and significantly increase the amount of packages available to you – which is a great thing. - -+ Also, users of popular distributions that do have many packages available in their repos, will enjoy the ability of installing packages that might not have behaved with their current set of installed libraries. - -For example, a Debian user who wants to install a package from ‘testing branch’ will not have to convert his entire system into ‘testing’ (in order for the package to run against newer libraries), rather, that user will simply be able to install only the package he wants from whichever branch he likes and on whatever branch he’s on. - -The latter point, was already basically possible for users who were compiling their packages straight from source, however, unless using a source based distribution such as Gentoo, most users will see this as just an unworthily hassle. - -+ The advanced user, or perhaps better put, the security aware user might feel more comfortable with this type of packages as long as they come from a reliable source as they tend to provide another layer of isolation since they are generally isolated from system packages. - -* Both S&F are being developed with enhanced security in mind, which generally makes use of “sandboxing” i.e isolation in order to prevent cases where they carry a virus which can infect the entire system, similar to the way .exe files on MS Windows may. (More on MS and S&F later) - -#### Developer Perspective - -For developers, the advantages of developing S&F packages will probably be a lot clearer than they are to the average user, some of these were already hinted in a previous section of this post. - -Nonetheless, here they are: - -+ S&F will make it easier on devs who want to develop for more than one Linux distribution by unifying the process of development, therefore minimizing the amount of work a developer needs to do in order to get his app running on multiple distributions. - -++ Developers could therefore gain easier access to a wider range of distributions. - -+ S&F allow devs to privately distribute their packages without being dependent on distribution maintainers to stabilize their package for each and every distro. - -++ Through the above, devs may gain access to direct statistics of user adoption / engagement for their software. - -++ Also through the above, devs could get more directly involved with users, rather than having to do so through a middleman, in this case, the distribution. - -### The Downsides - -– Bloat. Simple as that. Flatpak and Snap aren’t just magic making dependencies evaporate into thin air. Rather, instead of relying on the target system to provide the required dependencies, S&F comes with the dependencies prebuilt into them. - -As the saying goes “if the mountain won’t come to Muhammad, Muhammad must go to the mountain…” - -– Just as the security-aware user might enjoy S&F packages extra layer of isolation, as long they come from a trusted source. The less knowledgeable user on the hand, might be prone to the other side of the coin hazard which is using a package from an unknown source which may contain malicious software. - -The above point can be said to be valid even with today’s popular methods, as PPAs, overlays, etc might also be maintained by untrusted sources. - -However, with S&F packages the risk increases since malicious software developers need to create only one version of their program in order to infect a large number of distributions, whereas, without it they’d needed to create multiple versions in order to adjust their malware to other distributions. - -Was Microsoft Right All Along? - -With all that’s mentioned above in mind, it’s pretty clear that for the most part, the advantages of using S&F packages outweighs the drawbacks. - -At the least for users of binary-based distributions, or, non lightweight focused distros. - -Which eventually lead me to asking the above question – could it be that Microsoft was right all along? if so and S&F becomes the Linux standard, would you still consider Linux a Unix-like variant? - -Well apparently, the best one to answer those questions is probably time. - -Nevertheless, I’d argue that even if not entirely right, MS certainly has a good point to their credit, and having all these methods available here on Linux out of the box is certainly a plus in my book. - - --------------------------------------------------------------------------------- - -via: http://www.iwillfolo.com/ubuntus-snap-red-hats-flatpack-and-is-one-fits-all-linux-packages-useful/ - -作者:[Editorials][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: http://www.iwillfolo.com/category/editorials/ diff --git a/translated/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md b/translated/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md new file mode 100644 index 0000000000..7d4bbb6f25 --- /dev/null +++ b/translated/talk/20160625 Ubuntu’s Snap, Red Hat’s Flatpak And Is ‘One Fits All’ Linux Packages Useful.md @@ -0,0 +1,111 @@ +Ubuntu 的 Snap, Red Hat 的 Flatpak 这种通吃所有发行版的打包方式真的好用吗? +================================================================================= + +![](http://www.iwillfolo.com/wordpress/wp-content/uploads/2016/06/Flatpak-and-Snap-Packages.jpg) + +深入研究新一代的包开始进入到到Linux系统。 + +最近我们听到越来越多的消息是有关于 Ubuntu 的 Snap 包和 Red Hat 员工 Alexander Larsson 创造的 Flatpak (曾经叫做 xdg-app) + +这两种下一代打包方法在本质上拥有相同的目标和特点:即独立包装和不依赖于第三方系统函数库。 + +这种 Linux 新技术方向似乎自然会导致这样的问题:独立包的优点/缺点是什么?这是否让我们拥有更好的 Linux 系统?其背后的动机是什么? + +为了回答这些问题,让我们先深入了解一下 Snap 和 Flatpak。 + +### 动机 + +根据 [Flatpak][1] 和 [Snap][2] 的声明,背后的主要动机是使同一版本的应用程序能够运行在多个 Linux 发行版。 + +>“从一开始它的主要目标是允许相同的应用程序运行在各种 Linux 发行版和操作系统上。” Flatpak + +>“……‘snap’ 通用 Linux 包格式,使简单的二进制包能够完美的安全的运行在任何 Linux 桌面、服务器、云和设备上。” Snap + +说得更具体一点,站在 Snap 和 Flatpak (以下称之为S&F)背后的人认为,Linux 平台存在碎片化的障碍。 + +这个障碍导致了开发者们需要做许多不必要的工作来使他的软件能够运行在各种不同的发行版上,这影响了整个平台的前进。 + +所以,作为 Linux 发行版(Ubuntu 和 Red Hat)的领导者,他们希望消除这个障碍,推动平台发展。 + +但是,是否是更多的个人收益刺激了 S&F 的开发? + +#### 个人收益? + +虽然没有任何官方声明,但是试想一下,如果能够创造这种可能会被大多数发行版(可能不是全部)所采用的打包方式,那么这个项目的领导者将可能成为一个能够决定 Linux 大船航向的重要人物。 + +### 优势 + +这种独立包的好处多多,并且取决于不同的因素。 + +这些因素基本上可以归为两类: + +#### 用户角度 + ++ Liunx 用户的观点认为:Snap 和 Flatpak 带来了将任何应用安装在用户使用的任何发行版上的可能性。 + +例如你在使用一个不是很流行的发行版,由于开发工作的缺乏,它的 repo 只有很稀少的包。现在,通过 S&F 你就可以显著的增加包的数量,这是一个多么美好的事情。 + ++ 同样,对于使用流行的发行版的用户,即使该发行版的 repo 上有很多的包,他也可以在不改变它现有的函数库的同时安装一个新的包。 + +比方说, 一个 Debian 的用户想要安装一个 “测试分支” 的包,但是他又不想将他的整个系统变成测试版(以防该包需要运行在更新的函数库上)。现在,他可以简单的想安装哪个版本就安装哪个版本,而不需要考虑库的问题。 + +对于后者的观点,已经考虑到用户有可能使用源文件编译他们的包,然而,除非你使用基于源的发行版,否则大多数用户将从头编译视为是一个恶心到吐的事情。 + ++ 高级用户,或者称之为 “拥有安全意识的用户” 可能会觉得更容易接受这种类型的包,只要它们来自可靠来源,这种包倾向于提供另一层隔离,因为它们通常是与系统包想隔离的。 + +* 不论是 Snap 还是 Flatpak 都在不断努力增强它们的安全性,通常他们都使用 “沙盒” 来隔离,以防止它们可能携带病毒感染整个系统,就像微软 windows 系统中的 .exe 程序一样。(大多数在微软,而 S&F 紧随其后) + +#### 开发者角度 + +与普通用户相比,对于开发者来说,开发 S&F 包的优点可能更加清楚。这一点已经在上一节有所提示。 + +尽管如此,这些优点有: + ++ S&F 通过统一开发的过程,将多发行版的开发变得简单了起来。对于需要将他的应用运行在多个发行版的开发者来说,这大大的减少了他们的工作量。 + +++ 因此,开发者能够更容易的使他的应用运行在更多的设备上。 + ++ S&F 允许开发者私自发布他的包,不需要依靠发行版维护者在每一个发行版稳定发布他的包 + +++ 通过上述方法,开发者可以不依赖发行版而直接获取并统计用户的对其软件的采用和支持数据 +。 + +++ 同样是通过上述方法,开发者可以更好的直接与用户互动,而不需要通过中间媒介,比如发行版这种中间媒介。 + +### 缺点 + +– 膨胀. 就是这么简单. Flatpak 和 Snap 并不是凭空变出来他的依赖关系。相反,他是通过将依赖关系预构建在其中来代替使用系统中的依赖关系。 + +就像谚语说的:“如果山不过来,我们就去山上吧” + +– 之前提到安全意识强的用户会喜欢 S&F 提供的额外的一层隔离,只要该应用来自一个受信任的来源。但是从另外一个角度看,知识较少的用户,可能会从一个不靠谱的地方弄来一个包含恶意软件的包从而导致危害。 + +上面提到的观点可以说是有很有意义的,虽说今天的流行方法,像ppa、overlays等也可能是来自不受信任的来源。 + +但是,S&F 包更加增加这个风险,因为恶意软件开发者只需要开发一个版本就可以感染各种发行版。相反,如果没有 S&F,恶意软件的开发者就需要创建不同的版本以适应不同的发行版。 + +微软的方法都是正确的吗? + +考虑到上面提到的,很显然,在大多数情况下,使用 S&F 包的优点超过缺点。 + +至少对于二进制发行版的用户,或者非轻量级集中发行版的用户是这样。 + +这使我问出这个问题,可能微软一直是正确的吗?如果是的,那么当 S&F 变成 Linux 的标准后,你还会一如既往的使用 Linux 或者 类 Unix 系统吗? + +很显然,时间会是这个问题的最好答案。 + +不过,我得承认也许并不是全部正确的,但是从微软的信誉来看,它有着很好的观点,并且所有这些可以用在开箱即用的 Linux 上的可行的方法都将加在我的书中。 + +-------------------------------------------------------------------------------- + +via: http://www.iwillfolo.com/ubuntus-snap-red-hats-flatpack-and-is-one-fits-all-linux-packages-useful/ + +作者:[Editorials][a] +译者:[Chao-zhi](https://github.com/Chao-zhi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.iwillfolo.com/category/editorials/ +[1]: http://flatpak.org/press/2016-06-21-flatpak-released.html +[2]: https://insights.ubuntu.com/2016/06/14/universal-snap-packages-launch-on-multiple-linux-distros From bdd23462cf5d88f914cc5e5d7019a93c2e6e66e8 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:30:45 +0800 Subject: [PATCH 22/48] =?UTF-8?q?20160917-11=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ltiple Operating Systems on Local Network.md | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 sources/talk/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md diff --git a/sources/talk/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md b/sources/talk/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md new file mode 100644 index 0000000000..0210a50789 --- /dev/null +++ b/sources/talk/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md @@ -0,0 +1,98 @@ +NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network +==== + +One of the most important uses of a network is for file sharing purposes. There are multiple ways Linux and Windows, Mac OS X users on a network can now share files with each other and in this post, we shall cover Nitroshare, a cross-platform, open-source and easy-to-use application for sharing files across a local network. + +Nitroshare tremendously simplifies file sharing on a local network, once installed, it integrates with the operating system seamlessly. On Ubuntu, simply open it from the applications indicator, and on Windows, check it in the system tray. + +Additionally, it automatically detects every other device on a network that has Nitroshare installed thereby enabling a user to easily transfer files from one machine to another by selecting which device to transfer to. + +The following are the illustrious features of Nitroshare: + +- Cross-platform, runs on Linux, Windows and Mac OS X +- Easy to setup, no configurations required +- It’s simple to use +- Supports automatic discovery of devices running Nitroshare on local network +- Supports optional TSL encryption for security +- Works at high speeds on fast networks +- Supports transfer of files and directories (folders on Windows) +- Supports desktop notifications about sent files, connected devices and more + +The latest version of Nitroshare was developed using Qt 5, it comes with some great improvements such as: + +- Polished user interfaces +- Simplified device discovery process +- Removal of file size limitation from other versions +- Configuration wizard has also been removed to make it easy to use + +### How To Install Nitroshare on Linux Systems + + +NitroShare is developed to run on a wide variety of modern Linux distributions and desktop environments. + +#### On Debian Sid and Ubuntu 16.04+ + +NitroShare is included in the Debian and Ubuntu software repositories and can be easily installed with the following command. + +``` +$ sudo apt-get install nitroshare +``` + +But the available version might be out of date, however, to install the latest version of Nitroshare, issue the command below to add the PPA for the latest packages: + +``` +$ sudo apt-add-repository ppa:george-edison55/nitroshare +$ sudo apt-get update +$ sudo apt-get install nitroshare +``` + +#### On Fedora 24-23 + +Recently, NitroShare has been included to Fedora repositories and can be installed with the following command: + +``` +$ sudo dnf install nitroshare +``` + +#### On Arch Linux + +For Arch Linux, NitroShare packages are available from the AUR and can be built/installed with the following commands: + +``` +# wget https://aur.archlinux.org/cgit/aur.git/snapshot/nitroshare.tar.gz +# tar xf nitroshare.tar.gz +# cd nitroshare +# makepkg -sri +``` + +### How to Use NitroShare on Linux + +Note: As I had already mentioned earlier on, all other machines that you wish to share files with on the local network must have Nitroshare installed and running. + +After successfully installing it, search for Nitroshare in the system dash or system menu and launch it. + +![](http://www.tecmint.com/wp-content/uploads/2016/09/NitroShare-Send-Files.png) + +After selecting the files, click on “Open” to proceed to choosing the destination device as in the image below. Select the device and click “Ok” that is if you have any devices running Nitroshare on the local network. + +![](http://www.tecmint.com/wp-content/uploads/2016/09/NitroShare-Local-Devices.png) + +![](http://www.tecmint.com/wp-content/uploads/2016/09/NitroShare-File-Transfer-Progress.png) + +From the NitroShare settings – General tab, you can add the device name, set default downloads location and in Advance settings you can set port, buffer, timeout, etc. only if you needed. + +Homepage: + +That’s it for now, if you have any issues regarding Nitroshare, you can share with us using our comment section below. You can as well make suggestions and let us know of any wonderful, cross-platform file sharing applications out there that we probably have no idea about and always remember to stay connected to Tecmint. + +-------------------------------------------------------------------------------- + +via: http://linoxide.com/firewall/pfsense-setup-basic-configuration/ + +作者:[Aaron Kili][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/author/aaronkili/ From 2a880b9426d4644dadb3558c9b734029f3bce85a Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:31:35 +0800 Subject: [PATCH 23/48] =?UTF-8?q?20160917-11=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...are Files Between Multiple Operating Systems on Local Network.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sources/{talk => tech}/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md (100%) diff --git a/sources/talk/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md b/sources/tech/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md similarity index 100% rename from sources/talk/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md rename to sources/tech/20160914 NitroShare – Easily Share Files Between Multiple Operating Systems on Local Network.md From 518ee1e20ad341b4ae12e27410b8b39d70e98360 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:35:24 +0800 Subject: [PATCH 24/48] =?UTF-8?q?20160917-12=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...esktop in Ubuntu 16.04 and Fedora 22-24.md | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md diff --git a/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md b/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md new file mode 100644 index 0000000000..070d464077 --- /dev/null +++ b/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md @@ -0,0 +1,80 @@ +How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24 +==== + +Xfce is a modern, open source and lightweight desktop environment for Linux systems. It also works well on many other Unix-like systems such as Mac OS X, Solaris, *BSD plus several others. It is fast and also user friendly with a simple and elegant user interface. + +Installing a desktop environment on servers can sometimes prove helpful, as certain applications may require a desktop interface for efficient and reliable administration and one of the remarkable properties of Xfce is its low system resources utilization such as low RAM consumption, thereby making it a recommended desktop environment for servers if need be. + +### XFCE Desktop Features + +Additionally, some of its noteworthy components and features are listed below: + +- Xfwm windows manager +- Thunar file manager +- User session manger to deal with logins, power management and beyond +- Desktop manager for setting background image, desktop icons and many more +- An application manager +- It’s highly pluggable as well plus several other minor features + +The latest stable release of this desktop is Xfce 4.12, all its features and changes from previous versions are listed here. + +#### Install Xfce Desktop on Ubuntu 16.04 + +Linux distributions such as Xubuntu, Manjaro, OpenSUSE, Fedora Xfce Spin, Zenwalk and many others provide their own Xfce desktop packages, however, you can install the latest version as follows. + +``` +$ sudo apt update +$ sudo apt install xfce4 +``` + +Wait for the installation process to complete, then logout out of your current session or you can possibly restart your system as well. At the login interface, choose Xfce desktop and login as in the screen shot below: + +![](http://www.tecmint.com/wp-content/uploads/2016/09/Select-Xfce-Desktop-at-Login.png) + +![](http://www.tecmint.com/wp-content/uploads/2016/09/XFCE-Desktop.png) + +#### Install Xfce Desktop in Fedora 22-24 + +If you have an existing Fedora distribution and wanted to install xfce desktop, you can use yum or dnf to install it as shown. + +``` +-------------------- On Fedora 22 -------------------- +# yum install @xfce +-------------------- On Fedora 23-24 -------------------- +# dnf install @xfce-desktop-environment +``` + +After installing Xfce, you can choose the xfce login from the Session menu or reboot the system. + +![](http://www.tecmint.com/wp-content/uploads/2016/09/Select-Xfce-Desktop-at-Fedora-Login.png) + +![](http://www.tecmint.com/wp-content/uploads/2016/09/Install-Xfce-Desktop-in-Fedora.png) + +If you don’t want Xfce desktop on your system anymore, use the command below to uninstall it: + +``` +-------------------- On Ubuntu 16.04 -------------------- +$ sudo apt purge xfce4 +$ sudo apt autoremove +-------------------- On Fedora 22 -------------------- +# yum remove @xfce +-------------------- On Fedora 23-24 -------------------- +# dnf remove @xfce-desktop-environment +``` + +In this simple how-to guide, we walked through the steps for installation of latest version of Xfce desktop, which I believe were easy to follow. If all went well, you can enjoy using xfce, as one of the [best desktop environments for Linux systems][1]. + +However, to get back to us, you can use the feedback section below and remember to always stay connected to Tecmint. + +-------------------------------------------------------------------------------- + +via: http://linoxide.com/firewall/pfsense-setup-basic-configuration/ + +作者:[Aaron Kili ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/author/aaronkili/ +[1]: http://www.tecmint.com/best-linux-desktop-environments/ From 6cf2569553c666ba6fa170795e46cd7fd1c5d34a Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:38:34 +0800 Subject: [PATCH 25/48] =?UTF-8?q?20160917-13=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20160827 4 Best Linux Boot Loaders.md | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 sources/tech/20160827 4 Best Linux Boot Loaders.md diff --git a/sources/tech/20160827 4 Best Linux Boot Loaders.md b/sources/tech/20160827 4 Best Linux Boot Loaders.md new file mode 100644 index 0000000000..91deb14d58 --- /dev/null +++ b/sources/tech/20160827 4 Best Linux Boot Loaders.md @@ -0,0 +1,76 @@ +4 Best Linux Boot Loaders +==== + +When you turn on your machine, immediately after POST (Power On Self Test) is completed successfully, the BIOS locates the configured bootable media, and reads some instructions from the master boot record (MBR) or GUID partition table which is the first 512 bytes of the bootable media. The MBR contains two important sets of information, one is the boot loader and two, the partition table. + +### What is a Boot Loader? + +A boot loader is a small program stored in the MBR or GUID partition table that helps to load an operating system into memory. Without a boot loader, your operating system can not be loaded into memory. + +There are several boot loaders we can install together with Linux on our systems and in this article, we shall briefly talk about a handful of the best Linux boot loaders to work with. + +### 1. GNU GRUB + +GNU GRUB is a popular and probably the most used multiboot Linux boot loader available, based on the original GRUB (GRand Unified Bootlader) which was created by Eirch Stefan Broleyn. It comes with several improvements, new features and bug fixes as enhancements of the original GRUB program. + +Importantly, GRUB 2 has now replaced the GRUB. And notably, the name GRUB was renamed to GRUB Legacy and is not actively developed, however, it can be used for booting older systems since bug fixes are still on going. + +GRUB has the following prominent features: + +- Supports multiboot +- Supports multiple hardware architectures and operating systems such as Linux and Windows +- Offers a Bash-like interactive command line interface for users to run GRUB commands as well interact with configuration files +- Enables access to GRUB editor +- Supports setting of passwords with encryption for security +- Supports booting from a network combined with several other minor features + +Visit Homepage: + +### 2. LILO (Linux Loader) + +LILO is a simple yet powerful and stable Linux boot loader. With the growing popularity and use of GRUB, which has come with numerous improvements and powerful features, LILO has become less popular among Linux users. + +While it loads, the word “LILO” is displayed on the screen and each letter appears before or after a particular event has occurred. However, the development of LILO was stopped in December 2015, it has a number of features as listed below: + +- Does not offer an interactive command line interface +- Supports several error codes +- Offers no support for booting from a network +- All its files are stored in the first 1024 cylinders of a drive +- Faces limitation with BTFS, GPT and RAID plus many more. + +Visit Homepage: + +### 3. BURG – New Boot Loader + +Based on GRUB, BURG is a relatively new Linux boot loader. Because it is derived from GRUB, it ships in with some of the primary GRUB features, nonetheless, it also offers remarkable features such as a new object format to support multiple platforms including Linux, Windows, Mac OS, FreeBSD and beyond. + +Additionally, it supports a highly configurable text and graphical mode boot menu, stream plus planned future improvements for it to work with various input/output devices. + +Visit Homepage: + +### 4. Syslinux + +Syslinux is an assortment of light weight boot loaders that enable booting from CD-ROMs, from a network and so on. It supports filesystems such as FAT for MS-DOS, and ext2, ext3, ext4 for Linux. It as well supports uncompressed single-device Btrfs. + +Note that Syslinux only accesses files in its own partition, therefore, it does not offer multi-filesystem boot capabilities. + +Visit Homepage: + +### Conclusion + +A boot loader allows you to manage multiple operating systems on your machine and select which one to use at a particular time, without it, your machine can not load the kernel and the rest of the operating system files. + +Have we missed any tip-top Linux boot loader here? If so, then let us know by using the comment form below by making suggestions of any commendable boot loaders that can support Linux operating system. + + +-------------------------------------------------------------------------------- + +via: http://linoxide.com/firewall/pfsense-setup-basic-configuration/ + +作者:[Aaron Kili][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/best-linux-boot-loaders/ From e74771dbf9f9f54ea1c4f101ea4ea77b14cfcfc2 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:45:23 +0800 Subject: [PATCH 26/48] =?UTF-8?q?20160917-14=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Shell Commands and Their Usage in Linux.md | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 sources/tech/20160826 Understanding Different Classifications of Shell Commands and Their Usage in Linux.md diff --git a/sources/tech/20160826 Understanding Different Classifications of Shell Commands and Their Usage in Linux.md b/sources/tech/20160826 Understanding Different Classifications of Shell Commands and Their Usage in Linux.md new file mode 100644 index 0000000000..29b3bf04da --- /dev/null +++ b/sources/tech/20160826 Understanding Different Classifications of Shell Commands and Their Usage in Linux.md @@ -0,0 +1,219 @@ +Understanding Different Classifications of Shell Commands and Their Usage in Linux +==== + +When it comes to gaining absolute control over your Linux system, then nothing comes close to the command line interface (CLI). In order to become a Linux power user, one must understand the [different types of shell commands][1] and the appropriate ways of using them from the terminal. + +In Linux, there are several types of commands, and for a new Linux user, knowing the meaning of different commands enables for efficient and precise usage. Therefore, in this article, we shall walk through the various classifications of shell commands in Linux. + +One important thing to note is that the command line interface is different from the shell, it only provides a means for you to access the shell. The shell, which is also programmable then makes it possible to communicate with the kernel using commands. + +Different classifications of Linux commands fall under the following classifications: + +### 1. Program Executables (File System Commands) + +When you run a command, Linux searches through the directories stored in the $PATH environmental variable from left to right for the executable of that specific command. + +You can view the directories in the $PATH as follows: + +``` +$ echo $PATH +/home/aaronkilik/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games +``` + +In the above order, the directory /home/aaronkilik/bin will be searched first followed by /usr/local/sbin and so on, the order is significant in the search process. + +Examples of file system commands in /usr/bin directory: + +``` +$ ll /bin/ +``` + +Sample Output + +``` +total 16284 +drwxr-xr-x 2 root root 4096 Jul 31 16:30 ./ +drwxr-xr-x 23 root root 4096 Jul 31 16:29 ../ +-rwxr-xr-x 1 root root 6456 Apr 14 18:53 archdetect* +-rwxr-xr-x 1 root root 1037440 May 17 16:15 bash* +-rwxr-xr-x 1 root root 520992 Jan 20 2016 btrfs* +-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-calc-size* +lrwxrwxrwx 1 root root 5 Jul 31 16:19 btrfsck -> btrfs* +-rwxr-xr-x 1 root root 278376 Jan 20 2016 btrfs-convert* +-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-debug-tree* +-rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-find-root* +-rwxr-xr-x 1 root root 270136 Jan 20 2016 btrfs-image* +-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-map-logical* +-rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-select-super* +-rwxr-xr-x 1 root root 253816 Jan 20 2016 btrfs-show-super* +-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfstune* +-rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-zero-log* +-rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2* +-rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox* +-rwxr-xr-x 1 root root 31288 May 20 2015 bzcat* +lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzcmp -> bzdiff* +-rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff* +lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzegrep -> bzgrep* +-rwxr-xr-x 1 root root 4877 May 20 2015 bzexe* +lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzfgrep -> bzgrep* +-rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep* +``` + +### 2. Linux Aliases + +These are user defined commands, they are created using the alias shell built-in command, and contain other shell commands with some options and arguments. The ideas is to basically use new and short names for lengthy commands. + +The syntax for creating an alias is as follows: + +``` +$ alias newcommand='command -options' +``` + +To list all aliases on your system, issue the command below: + +``` +$ alias -p +alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' +alias egrep='egrep --color=auto' +alias fgrep='fgrep --color=auto' +alias grep='grep --color=auto' +alias l='ls -CF' +alias la='ls -A' +alias ll='ls -alF' +alias ls='ls --color=auto' +``` + +To create a new alias in Linux, go through some below examples. + +``` +$ alias update='sudo apt update' +$ alias upgrade='sudo apt dist-upgrade' +$ alias -p | grep 'up' +``` + +![](http://www.tecmint.com/wp-content/uploads/2016/08/Create-Aliase-in-Linux.png) + +However, the aliases we have created above only work temporarily, when the system is restarted, they will not work after the next boot. You can set permanent aliases in your `.bashrc` file as shown below. + +![](http://www.tecmint.com/wp-content/uploads/2016/08/Set-Linux-Aliases-Permanent.png) + +After adding them, run the command below to active. + +``` +$ source ~/.bashrc +``` + +### 3. Linux Shell Reserved Words + +In shell programming, words such as if, then, fi, for, while, case, esac, else, until and many others are shell reserved words. As the description implies, they have specialized meaning to the shell. + +You can list out all Linux shell keywords using type command as shown: + +``` +$ type if then fi for while case esac else until +if is a shell keyword +then is a shell keyword +fi is a shell keyword +for is a shell keyword +while is a shell keyword +case is a shell keyword +esac is a shell keyword +else is a shell keyword +until is a shell keyword +``` + +Suggested Read: 10 Useful Linux Chaining Operators with Practical Examples + +### 4. Linux Shell Functions + +A shell function is a group of commands that are executed collectively within the current shell. Functions help to carry out a specific task in a shell script. The conventional form of writing shell functions in a script is: + +``` +function_name() { +command1 +command2 +……. +} +``` + +Alternatively, + +``` +function function_name { +command1 +command2 +……. +} +``` + +Let’s take a look at how to write shell functions in a script named shell_functions.sh. + +``` +#!/bin/bash +#write a shell function to update and upgrade installed packages +upgrade_system(){ +sudo apt update; +sudo apt dist-upgrade; +} +#execute function +upgrade_system +``` + +Instead of executing the two commands: sudo apt update and sudo apt dist-upgrade from the command line, we have written a simple shell function to execute the two commands as a single command, upgrade_system within a script. + +Save the file and thereafter, make the script executable. Finally run it as below: + +``` +$ chmod +x shell_functions.sh +$ ./shell_functions.sh +``` + +![](http://www.tecmint.com/wp-content/uploads/2016/08/Linux-Shell-Functions-Script.png) + +### 5. Linux Shell Built-in Commands + +These are Linux commands that built into the shell, thus you cannot find them within the file system. They include pwd, cd, bg, alias, history, type, source, read, exit and many others. + +You can list or check Linux built-in commands using type command as shown: + +``` +$ type pwd +pwd is a shell builtin +$ type cd +cd is a shell builtin +$ type bg +bg is a shell builtin +$ type alias +alias is a shell builtin +$ type history +history is a shell builtin +``` + +Learn about some Linux built-in Commands usage: + +- [15 ‘pwd’ Command Examples in Linux][2] +- [15 ‘cd’ Command Examples in Linux][3] +- [Learn The Power of Linux ‘history’ Command][4] + +### Conclusion + +As a Linux user, it is always important to know the type of command you are running. I believe, with the precise and simple-to-understand explanation above including a few relevant illustrations, you probably have a good understanding of the [various categories of Linux commands][5]. + +You can as well get in tough through the comment section below for any questions or supplementary ideas that you would like to offer us. + +-------------------------------------------------------------------------------- + +via: http://linoxide.com/firewall/pfsense-setup-basic-configuration/ + +作者:[Aaron Kili ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/author/aaronkili/ +[1]: http://www.tecmint.com/different-types-of-linux-shells/ +[2]: http://www.tecmint.com/pwd-command-examples/ +[3]: http://www.tecmint.com/cd-command-in-linux/ +[4]: http://www.tecmint.com/history-command-examples/ +[5]: http://www.tecmint.com/category/linux-commands/ From de1d6ba687e5bd81d7e39918b3162497ad305567 Mon Sep 17 00:00:00 2001 From: Bian Jiaping Date: Sat, 17 Sep 2016 21:48:21 +0800 Subject: [PATCH 27/48] [Translating] It's time to make LibreOffice and OpenOffice one again --- ...14 It's time to make LibreOffice and OpenOffice one again.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md b/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md index 3aba30f29b..d91d08d03a 100644 --- a/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md +++ b/sources/tech/20160914 It's time to make LibreOffice and OpenOffice one again.md @@ -1,3 +1,5 @@ +Translating by bianjp + It's time to make LibreOffice and OpenOffice one again ========== From 05fe2f738345dd06976e11fba2a504e596880c8f Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:52:24 +0800 Subject: [PATCH 28/48] =?UTF-8?q?20160917-15=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...w to Use Flow Control Statements in Awk.md | 246 ++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 sources/tech/awk/part 12 - How to Use Flow Control Statements in Awk.md diff --git a/sources/tech/awk/part 12 - How to Use Flow Control Statements in Awk.md b/sources/tech/awk/part 12 - How to Use Flow Control Statements in Awk.md new file mode 100644 index 0000000000..37bd91461f --- /dev/null +++ b/sources/tech/awk/part 12 - How to Use Flow Control Statements in Awk.md @@ -0,0 +1,246 @@ +How to Use Flow Control Statements in Awk - part12 +==== + +When you review all the Awk examples we have covered so far, right from the start of the Awk series, you will notice that all the commands in the various examples are executed sequentially, that is one after the other. But in certain situations, we may want to run some text filtering operations based on some conditions, that is where the approach of flow control statements sets in. + +![](http://www.tecmint.com/wp-content/uploads/2016/08/Use-Flow-Control-Statements-in-Awk.png) + +There are various flow control statements in Awk programming and these include: + +- if-else statement +- for statement +- while statement +- do-while statement +- break statement +- continue statement +- next statement +- nextfile statement +- exit statement + +However, for the scope of this series, we shall expound on: if-else, for, while and do while statements. Remember that we already walked through how to use next statement in Part 6 of this Awk series. + +### 1. The if-else Statement + +The expected syntax of the if statement is similar to that of the shell if statement: + +``` +if (condition1) { +actions1 +} +else { +actions2 +} +``` + +In the above syntax, condition1 and condition2 are Awk expressions, and actions1 and actions2 are Awk commands executed when the respective conditions are satisfied. + +When condition1 is satisfied, meaning it’s true, then actions1 is executed and the if statement exits, otherwise actions2 is executed. + +The if statement can also be expanded to a if-else_if-else statement as below: + +``` +if (condition1){ +actions1 +} +else if (conditions2){ +actions2 +} +else{ +actions3 +} +``` + +For the form above, if condition1 is true, then actions1 is executed and the if statement exits, otherwise condition2 is evaluated and if it is true, then actions2 is executed and the if statement exits. However, when condition2 is false then, actions3 is executed and the if statement exits. + +Here is a case in point of using if statements, we have a list of users and their ages stored in the file, users.txt. + +We want to print a statement indicating a user’s name and whether the user’s age is less or more than 25 years old. + +``` +aaronkilik@tecMint ~ $ cat users.txt +Sarah L 35 F +Aaron Kili 40 M +John Doo 20 M +Kili Seth 49 M +``` + +We can write a short shell script to carry out our job above, here is the content of the script: + +``` +#!/bin/bash +awk ' { +if ( $3 <= 25 ){ +print "User",$1,$2,"is less than 25 years old." ; +} +else { +print "User",$1,$2,"is more than 25 years old" ; +} +}' ~/users.txt +``` + +Then save the file and exit, make the script executable and run it as follows: + +``` +$ chmod +x test.sh +$ ./test.sh +``` + +Sample Output + +``` +User Sarah L is more than 25 years old +User Aaron Kili is more than 25 years old +User John Doo is less than 25 years old. +User Kili Seth is more than 25 years old +``` + +### 2. The for Statement + +In case you want to execute some Awk commands in a loop, then the for statement offers you a suitable way to do that, with the syntax below: + +Here, the approach is simply defined by the use of a counter to control the loop execution, first you need to initialize the counter, then run it against a test condition, if it is true, execute the actions and finally increment the counter. The loop terminates when the counter does not satisfy the condition. + +``` +for ( counter-initialization; test-condition; counter-increment ){ +actions +} +``` + +The following Awk command shows how the for statement works, where we want to print the numbers 0-10: + +``` +$ awk 'BEGIN{ for(counter=0;counter<=10;counter++){ print counter} }' +``` + +Sample Output + +``` +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +### 3. The while Statement + +The conventional syntax of the while statement is as follows: + +``` +while ( condition ) { +actions +} +``` + +The condition is an Awk expression and actions are lines of Awk commands executed when the condition is true. + +Below is a script to illustrate the use of while statement to print the numbers 0-10: + +``` +#!/bin/bash +awk ' BEGIN{ counter=0 ; +while(counter<=10){ +print counter; +counter+=1 ; +} +} +``` + +Save the file and make the script executable, then run it: + +``` +$ chmod +x test.sh +$ ./test.sh +``` + +Sample Output + +``` +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +### 4. The do while Statement + +It is a modification of the while statement above, with the following underlying syntax: + +``` +do { +actions +} +while (condition) +``` + +The slight difference is that, under do while, the Awk commands are executed before the condition is evaluated. Using the very example under while statement above, we can illustrate the use of do while by altering the Awk command in the test.sh script as follows: + +``` +#!/bin/bash +awk ' BEGIN{ counter=0 ; +do{ +print counter; +counter+=1 ; +} +while (counter<=10) +} +' +``` + +After modifying the script, save the file and exit. Then make the script executable and execute it as follows: + +``` +$ chmod +x test.sh +$ ./test.sh +``` + +Sample Output + +``` +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +### Conclusion + +This is not a comprehensive guide regarding Awk flow control statements, as I had mentioned earlier on, there are several other flow control statements in Awk. + +Nonetheless, this part of the Awk series should give you a clear fundamental idea of how execution of Awk commands can be controlled based on certain conditions. + +You can as well expound more on the rest of the flow control statements to gain more understanding on the subject matter. Finally, in the next section of the Awk series, we shall move into writing Awk scripts. + +-------------------------------------------------------------------------------- + +via: http://www.tecmint.com/use-flow-control-statements-with-awk-command/ + +作者:[Aaron Kili][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/author/aaronkili/ + + From 6e2e5976065f885d2e6df303a43fe48126a605dd Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 21:56:44 +0800 Subject: [PATCH 29/48] =?UTF-8?q?20160917-16=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... Scripts Using Awk Programming Language.md | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 sources/tech/awk/Part 13 - How to Write Scripts Using Awk Programming Language.md diff --git a/sources/tech/awk/Part 13 - How to Write Scripts Using Awk Programming Language.md b/sources/tech/awk/Part 13 - How to Write Scripts Using Awk Programming Language.md new file mode 100644 index 0000000000..05ef0902fe --- /dev/null +++ b/sources/tech/awk/Part 13 - How to Write Scripts Using Awk Programming Language.md @@ -0,0 +1,160 @@ +Part 13 - How to Write Scripts Using Awk Programming Language +==== + +All along from the beginning of the Awk series up to Part 12, we have been writing small Awk commands and programs on the command line and in shell scripts respectively. + +However, Awk, just as Shell, is also an interpreted language, therefore, with all that we have walked through from the start of this series, you can now write Awk executable scripts. + +Similar to how we write a shell script, Awk scripts start with the line: + +``` +#! /path/to/awk/utility -f +``` + +For example on my system, the Awk utility is located in /usr/bin/awk, therefore, I would start an Awk script as follows: + +``` +#! /usr/bin/awk -f +``` + +Explaining the line above: + +``` +#! – referred to as Shebang, which specifies an interpreter for the instructions in a script +/usr/bin/awk – is the interpreter +-f – interpreter option, used to read a program file +``` + +That said, let us now dive into looking at some examples of Awk executable scripts, we can start with the simple script below. Use your favorite editor to open a new file as follows: + +``` +$ vi script.awk +``` + +And paste the code below in the file: + +``` +#!/usr/bin/awk -f +BEGIN { printf "%s\n","Writing my first Awk executable script!" } +``` + +Save the file and exit, then make the script executable by issuing the command below: + +``` +$ chmod +x script.awk +``` + +Thereafter, run it: + +``` +$ ./script.awk +``` + +Sample Output + +``` +Writing my first Awk executable script! +``` + +A critical programmer out there must be asking, “where are the comments?”, yes, you can also include comments in your Awk script. Writing comments in your code is always a good programming practice. + +It helps other programmers looking through your code to understand what you are trying to achieve in each section of a script or program file. + +Therefore, you can include comments in the script above as follows. + +``` +#!/usr/bin/awk -f +#This is how to write a comment in Awk +#using the BEGIN special pattern to print a sentence +BEGIN { printf "%s\n","Writing my first Awk executable script!" } +``` + +Next, we shall look at an example where we read input from a file. We want to search for a system user named aaronkilik in the account file, /etc/passwd, then print the username, user ID and user GID as follows: + +Below is the content of our script called second.awk. + +``` +#! /usr/bin/awk -f +#use BEGIN sepecial character to set FS built-in variable +BEGIN { FS=":" } +#search for username: aaronkilik and print account details +/aaronkilik/ { print "Username :",$1,"User ID :",$3,"User GID :",$4 } +``` + +Save the file and exit, make the script executable and execute it as below: + +``` +$ chmod +x second.awk +$ ./second.awk /etc/passwd +``` + +Sample Output + +``` +Username : aaronkilik User ID : 1000 User GID : 1000 +``` + +In the last example below, we shall use do while statement to print out numbers from 0-10: + +Below is the content of our script called do.awk. + +``` +#! /usr/bin/awk -f +#printing from 0-10 using a do while statement +#do while statement +BEGIN { +#initialize a counter +x=0 +do { +print x; +x+=1; +} +while(x<=10) +} +``` + +After saving the file, make the script executable as we have done before. Afterwards, run it: + +``` +$ chmod +x do.awk +$ ./do.awk +``` + +Sample Output + +``` +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +### Summary + +We have come to the end of this interesting Awk series, I hope you have learned a lot from all the 13 parts, as an introduction to Awk programming language. + +As I mentioned from the beginning, Awk is a complete text processing language, for that reason, you can learn more other aspects of Awk programming language such as environmental variables, arrays, functions (built-in & user defined) and beyond. + +There is yet additional parts of Awk programming to learn and master, so, below, I have provided some links to important online resources that you can use to expand your Awk programming skills, these are not necessarily all that you need, you can also look out for useful Awk programming books. + + +For any thoughts you wish to share or questions, use the comment form below. Remember to always stay connected to Tecmint for more exciting series. + +-------------------------------------------------------------------------------- + +via: http://www.tecmint.com/write-shell-scripts-in-awk-programming/ + +作者:[Aaron Kili |][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/author/aaronkili/ From fd26bb6a7bb274b48bf70174ffad4bdbf2c64a0c Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:05:24 +0800 Subject: [PATCH 30/48] =?UTF-8?q?20160917-17=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...sing Ansible to Provision Vagrant Boxes.md | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 sources/tech/20160915 Using Ansible to Provision Vagrant Boxes.md diff --git a/sources/tech/20160915 Using Ansible to Provision Vagrant Boxes.md b/sources/tech/20160915 Using Ansible to Provision Vagrant Boxes.md new file mode 100644 index 0000000000..57003390d8 --- /dev/null +++ b/sources/tech/20160915 Using Ansible to Provision Vagrant Boxes.md @@ -0,0 +1,159 @@ +Using Ansible to Provision Vagrant Boxes +==== + +![](https://i1.wp.com/cdn.fedoramagazine.org/wp-content/uploads/2016/08/vagrant-plus-ansible.jpg?w=1352&ssl=1) + +Ansible is a great tool for system administrators who want to automate system administration tasks. From configuration management to provisioning and managing containers for application deployments, Ansible [makes it easy][1]. The lightweight module based architecture is ideal for system administration. One advantage is that when the node is not being managed by Ansible, no resources are used. + +This article covers how to use Ansible to provision Vagrant boxes. A [Vagrant box][2] in simple terms is a virtual machine prepackaged with tools required to run the development environment. You can use these boxes to distribute the development environment used by other team members for project work. Using Ansible, you can automate provisioning the Vagrant boxes with your development packages. + +This tutorial uses Fedora 24 as the host system and [CentOS][3] 7 as the Vagrant box. + +### Setting up prerequisites + +To configure Vagrant boxes using Ansible, you’ll need a few things setup. This tutorial requires you to install Ansible and Vagrant on the host machine. On your host machine, execute the following command to install the tools: + +``` +sudo dnf install ansible vagrant vagrant-libvirt +``` + +The above command installs both Ansible and Vagrant on your host system, along with Vagrant’s libvirt provider. Vagrant doesn’t provide functionality to host your virtual machine guests (VMs). Rather, it depends on third party providers such as libvirt, VirtualBox, VMWare, etc. to host the VMs. This provider works directly with libvirt and KVM on your Fedora system. + +Next, make sure your user is in the wheel group. This special group allows you to run system administration commands. If you created your user as an administrator, such as during installation, you’ll have this group membership. Run the following command: + +``` +id | grep wheel +``` + +If you see output, your user is in the group, and you can move on to the next section. If not, run the following command. You’ll need to provide the password for the root account. Substitute your user name for the text : + +``` +su -c 'usermod -a -G wheel ' +``` + +Then you will need to logout, and log back in, to inherit the group membership properly. + +Now it’s time to create your first Vagrant box, which you’ll then configure using Ansible. + +### Setting up the Vagrant box + +Before you use Ansible to provision a box, you must create the box. To start, create a new directory which will store files related to the Vagrant box. To create this directory and make it the current working directory, issue the following command: + +``` +mkdir -p ~/lampbox && cd ~/lampbox +``` + +Before you create the box, you should understand the goal. This box is a simple example that runs CentOS 7 as its base system, along with the Apache web server, MariaDB (the popular open source database server from the original developers of MySQL) and PHP. + +To initialize the Vagrant box, use the vagrant init command: + +``` +vagrant init centos/7 +``` + +This command initializes the Vagrant box and creates a file named Vagrantfile, with some pre-configured variables. Open this file so you can modify it. The following line lists the base box used by this configuration. + +``` +config.vm.box = "centos/7" +``` + +Now setup port forwarding, so after you finish setup and the Vagrant box is running, you can test the server. To setup port forwarding, add the following line just before the end statement in Vagrantfile: + +``` +config.vm.network "forwarded_port", guest: 80, host: 8080 +``` + +This option maps port 80 of the Vagrant Box to port 8080 of the host machine. + +The next step is to set Ansible as our provisioning provider for the Vagrant Box. Add the following lines before the end statement in your Vagrantfile to set Ansible as the provisioning provider: + +``` +config.vm.provision :ansible do |ansible| + ansible.playbook = "lamp.yml" +end +``` + +(You must add all three lines before the final end statement.) Notice the statement ansible.playbook = “lamp.yml”. This statement defines the name of the playbook used to provision the box. + +### Creating the Ansible playbook + +In Ansible, playbooks describe a policy to be enforced on your remote nodes. Put another way, playbooks manage configurations and deployments on remote nodes. Technically speaking, a playbook is a YAML file in which you write tasks to perform on remote nodes. In this tutorial, you’ll create a playbook named lamp.yml to provision the box. + +To make the playbook, create a file named lamp.yml in the same directory where your Vagrantfile is located and add the following lines to it: + +``` +--- +- hosts: all + become: yes + become_user: root + tasks: + - name: Install Apache + yum: name=httpd state=latest + - name: Install MariaDB + yum: name=mariadb-server state=latest + - name: Install PHP5 + yum: name=php state=latest + - name: Start the Apache server + service: name=httpd state=started + - name: Install firewalld + yum: name=firewalld state=latest + - name: Start firewalld + service: name=firewalld state=started + - name: Open firewall + command: firewall-cmd --add-service=http --permanent +``` + +An explanation of each line of lamp.yml follows. + +- hosts: all specifies the playbook should run over every host defined in the Ansible configuration. Since no hosts are configured hosts yet, the playbook will run on localhost. +- sudo: true states the tasks should be performed with root privileges. +- tasks: specifies the tasks to perform when the playbook runs. Under the tasks section: +- - name: … provides a descriptive name to the task +- - yum: … specifies the task should be executed by the yum module. The options name and state are key=value pairs for use by the yum module. + +When this playbook executes, it installs the latest versions of the Apache (httpd) web server, MariaDB, and PHP. Then it installs and starts firewalld, and opens a port for the Apache server. You’re now done writing the playbook for the box. Now it’s time to provision it. + +### Provisioning the box + +A few final steps remain before using the Vagrant Box provisioned using Ansible. To run this provisioning, execute the following command: + +``` +vagrant up --provider libvirt +``` + +The above command starts the Vagrant box, downloads the base box image to the host system if not already present, and then runs the playbook lamp.yml to provision. + +If everything works fine, the output looks somewhat similar to this example: + +![](https://i1.wp.com/cdn.fedoramagazine.org/wp-content/uploads/2016/08/vagrant-ansible-playbook-run.png?w=574&ssl=1) + +This output shows that the box has been provisioned. Now check whether the server is accessible. To confirm, open your web browser on the host machine and point it to the address http://localhost:8080. Remember, port 8080 of the local host is forwarded to port 80 of the Vagrant box. You should be greeted with the Apache welcome page like the one shown below: + +![](https://i0.wp.com/cdn.fedoramagazine.org/wp-content/uploads/2016/08/vagrant-ansible-apache-up.png?w=1004&ssl=1) + +To make changes to your Vagrant box, first edit the Ansible playbook lamp.yml. You can find plentiful documentation on Ansible at [its official website][4]. Then run the following command to re-provision the box: + +``` +vagrant provision +``` + +### Conclusion + +You’ve now seen how to use Ansible to provision Vagrant boxes. This was a basic example, but you can use these tools for many other use cases. For example, you can deploy complete applications along with up-to-date version of required tools. Be creative as you use Ansible to provision your remote nodes or containers. + + +-------------------------------------------------------------------------------- + +via: https://fedoramagazine.org/using-ansible-provision-vagrant-boxes/ + +作者:[Saurabh Badhwar][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://h4xr.id.fedoraproject.org/ +[1]: https://ansible.com/ +[2]: https://www.vagrantup.com/ +[3]: https://centos.org/ +[4]: http://docs.ansible.com/ansible/index.html From ea4ae2d514b30bc7d2ae07e1c99bd1e51c513248 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:08:38 +0800 Subject: [PATCH 31/48] =?UTF-8?q?20160917-18=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rkdown in WordPress to Improve Workflow.md | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sources/tech/20160913 How to Use Markdown in WordPress to Improve Workflow.md diff --git a/sources/tech/20160913 How to Use Markdown in WordPress to Improve Workflow.md b/sources/tech/20160913 How to Use Markdown in WordPress to Improve Workflow.md new file mode 100644 index 0000000000..a199eddabe --- /dev/null +++ b/sources/tech/20160913 How to Use Markdown in WordPress to Improve Workflow.md @@ -0,0 +1,58 @@ +How to Use Markdown in WordPress to Improve Workflow +==== + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/markdown-wordpress-featured-2.jpg) + +Markdown is a simple markup language that helps you format your plain text documents with minimal effort. You may be used to formatting your articles using HTML or the Visual Editor in WordPress, but using markdown makes formatting a lot easier, and you can always export it to several formats including (but not limited to) HTML. + +WordPress does not come with native markdown support, but there are plugins that can add this functionality to your website if you so desire. + +In this tutorial I will demonstrate how to use the popular WP-Markdown plugin to add markdown support to a WordPress website. + +### Installation + +You can install this plugin directly by navigating to “Plugins -> Add New” and entering “[wp-markdown][1]” in the search box provided. The plugin should appear as the first option on the list. Click “Install Now” to install it. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/markdown-wordpress-install-plugin-1.png) + +### Configuration + +Once you have installed the plugin and activated it, navigate to “Settings -> Writing” in the menu and scroll down until you get to the markdown section. + +You can enable markdown support in posts, pages and comments. You can also enable a help bar for your post editor or comments which could be handy if you’re just learning the markdown syntax. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/markdown-wordpress-configuration.png) + +If you include code snippets in your blog posts, enabling the “Prettify syntax highlighter” option will automatically provide syntax highlighting for your code snippets. + +Once you are satisfied with your selections, click “Save Changes” to save your settings. + +### Write your posts with Markdown + +Once you have enabled markdown support on your website, you can start using it right away. + +Create a new post by going to “Posts -> Add New.” You will notice that the default Visual and Plain Text editors have been replaced by the markdown editor. + +If you did not enable the markdown help bar in the configuration options, you will not see a live preview of your formatted markdown. Nonetheless, as long as your syntax is correct, your markdown will be converted to valid HTML when you save or publish the post. + +However, if you’re a beginner to markdown and the live preview feature is important to you, simply go back to the settings to enable the help bar option, and you will get a nice live preview area at the bottom of your posts. In addition, you also get some buttons on top that will help you quickly insert markdown syntax into your posts. This could be a potentially amazing setting if people use it. You can adjust the priority of the notification on individual apps. This will let you choose what you see in the notification bar. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/08/markdown-wordpress-create-post.png) + +### Wrap up + +As you can see adding markdown support to a WordPress website is really easy, and it will only take a few minutes of your time. If you are completely new to markdown, you might also check out our [markdown cheatsheet][2] which provides a comprehensive reference to the markdown syntax. + +-------------------------------------------------------------------------------- + +via: https://www.maketecheasier.com/use-markdown-in-wordpress/?utm_medium=feed&utm_source=feedpress.me&utm_campaign=Feed%3A+maketecheasier + +作者:[Ayo Isaiah][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.maketecheasier.com/author/ayoisaiah/ +[1]: https://wordpress.org/plugins/wp-markdown/ +[2]: https://www.maketecheasier.com/productive-with-markdown-cheatsheet/ From b989a9598bbb44ada270021f01759ca819a3ce5b Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:12:51 +0800 Subject: [PATCH 32/48] =?UTF-8?q?20160917-19=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...reveals his favorite programming laptop.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 sources/talk/20160913 Linus Torvalds reveals his favorite programming laptop.md diff --git a/sources/talk/20160913 Linus Torvalds reveals his favorite programming laptop.md b/sources/talk/20160913 Linus Torvalds reveals his favorite programming laptop.md new file mode 100644 index 0000000000..9741a58fcc --- /dev/null +++ b/sources/talk/20160913 Linus Torvalds reveals his favorite programming laptop.md @@ -0,0 +1,62 @@ +Linus Torvalds reveals his favorite programming laptop +==== + +>It's the Dell XPS 13 Developer Edition. Here's why. + +I recently talked with some Linux developers about what the best laptop is for serious programmers. As a result I checked out several laptops from a programmer's viewpoint. The winner in my book? The 2016 Dell XPS 13 Developer Edition. I'm in good company. Linus Torvalds, Linux's creator, agrees. The Dell XPS 13 Developer Edition, for him, is the best laptop around. + +![](http://zdnet3.cbsistatic.com/hub/i/r/2016/07/18/702609c3-db38-4603-9f5f-4dcc3d71b140/resize/770xauto/50a8ba1c2acb1f0994aec2115d2e55ce/2016-dell-xps-13.jpg) + +Torvald's requirements may not be yours though. + +On Google+, Torvalds explained, "First off: [I don't use my laptop as a desktop replacement][1], and I only travel for a small handful of events each year. So for me, the laptop is a fairly specialized thing that doesn't get daily (or even weekly) use, so the main criteria are not some kind of "average daily use", but very much "travel use". + +Therefore, for Torvalds, "I end up caring a lot about it being fairly small and light, because I may end up carrying it around all day at a conference. I also want it to have a good screen, because by now I'm just used to it at my main desktop, and I want my text to be legible but small." + +The Dell's display is powered by Intel's Iris 540 GPU. In my experience it works really well. + +The Iris powers a 13.3 inch display with a 3,200×1,800 touchscreen. That's 280 pixels per inch, 40 more than my beloved [2015 Chromebook Pixel][2] and 60 more than a [MacBook Pro with Retina][3]. + +However, getting that hardware to work and play well with the [Gnome][4] desktop isn't easy. As Torvalds explained in another post, it "has the [same resolution as my desktop][5], but apparently because the laptop screen is smaller, Gnome seems to decide on its own that I need an automatic scaling factor of 2, which blows up all the stupid things (window decorations, icons etc) to a ridiculous degree". + +The solution? You can forget about looking to the user interface. You need to go to the shell and run: gsettings set org.gnome.desktop.interface scaling-factor 1. + +Torvalds may use Gnome, but he's [never liked the Gnome 3.x family much][6]. I can't argue with him. That's why I use [Cinnamon][7] instead. + +He also wants "a reasonably powerful CPU, because when I'm traveling I still build the kernel a lot. I don't do my normal full 'make allmodconfig' build between each pull request like I do at home, but I'd like to do it more often than I did with my previous laptop, which is actually (along with the screen) the main reason I wanted to upgrade." + +Linus doesn't describe the features of his XPS 13, but my review unit was a high-end model. It came with dual-core, 2.2GHz 6th Generation Intel Core i7-6560U Skylake processor and 16GBs of DDR3 RAM with a half a terabyte, PCIe solid state drive (SSD). I'm sure Torvalds' system is at least that well-equipped. + +Some features you may care about aren't on Torvalds' list. + +>"What I don't tend to care about is touch-screens, because my fingers are big and clumsy compared to the text I'm looking at (I also can't handle the smudges: maybe I just have particularly oily fingers, but I really don't want to touch that screen). + +I also don't care deeply about some 'all day battery life', because quite frankly, I can't recall the last time I didn't have access to power. I might not want to bother to plug it in for some quick check, but it's just not a big overwhelming issue. By the time battery life is in 'more than a couple of hours', I just don't care very much any more." +Dell claims the XPS 13, with its 56wHR, 4-Cell Battery, has about a 12-hour battery life. It has well over 10 in my experience. I haven't tried to run it down to the dregs. + +Torvalds also didn't have any trouble with the Intel Wi-Fi set. The non Developer Edition uses a Broadcom chip set and that has proven troublesome for both Windows and Linux users. Dell technical support was extremely helpful to me in getting this problem under control. + +Some people have trouble with the XPS 13 touchpad. Neither I nor Torvalds have any worries. Torvalds wrote, the "XPS13 touchpad works very well for me. That may be a personal preference thing, but it seems to be both smooth and responsive." + +Still, while Torvalds likes the XPS 13, he's also fond of the latest Lenovo X1 Carbon, HP Spectre 13 x360, and last year's Lenovo Yoga 900. Me? I like the XPS 13 Developer Editor. The price tag, which for the model I reviewed was $1949.99, may keep you from reaching for your credit card. + +Still, if you want to develop like one of the world's top programmers, the Dell XPS 13 Developer Edition is worth the money. + +-------------------------------------------------------------------------------- + +via: http://www.zdnet.com/article/linus-torvalds-reveals-his-favorite-programming-laptop/ + +作者:[Steven J. Vaughan-Nichols ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.zdnet.com/meet-the-team/us/steven-j-vaughan-nichols/ +[1]: https://plus.google.com/+LinusTorvalds/posts/VZj8vxXdtfe +[2]: http://www.zdnet.com/article/the-best-chromebook-ever-the-chromebook-pixel-2015/ +[3]: http://www.zdnet.com/product/apple-15-inch-macbook-pro-with-retina-display-mid-2015/ +[4]: https://www.gnome.org/ +[5]: https://plus.google.com/+LinusTorvalds/posts/d7nfnWSXjfD +[6]: http://www.zdnet.com/article/linus-torvalds-finds-gnome-3-4-to-be-a-total-user-experience-design-failure/ +[7]: http://www.zdnet.com/article/how-to-customise-your-linux-desktop-cinnamon/ From f6ff62bf57f73d351b8b3e173c109f2353eab383 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:15:30 +0800 Subject: [PATCH 33/48] =?UTF-8?q?20160917-20=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...You Should Be Using It instead of Slack.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 sources/tech/20160913 Ryver: Why You Should Be Using It instead of Slack.md diff --git a/sources/tech/20160913 Ryver: Why You Should Be Using It instead of Slack.md b/sources/tech/20160913 Ryver: Why You Should Be Using It instead of Slack.md new file mode 100644 index 0000000000..f4f4c54fcb --- /dev/null +++ b/sources/tech/20160913 Ryver: Why You Should Be Using It instead of Slack.md @@ -0,0 +1,62 @@ +Ryver: Why You Should Be Using It instead of Slack +===== + +It seems like everyone has heard of Slack, a team communication tool that can be used across multiple platforms to stay in the loop. It has revolutionised the way users discuss and plan projects, and it’s a clear upgrade to emails. + +I work in small writing teams, and I’ve never had a problem with communicating with others on my phone or computer while using it. If you want to keep up to date with team of any size, it’s a great way to stay in the loop. + +So, why are we here? Ryver is supposed to be the next big thing, offering an upgraded service in comparison to Slack. It’s completely free, and they’re pushing for a larger share of the market. + +Is it good enough to be a Slack-Killer? What are the differences between two similar sounding services? + +Read on to find out more. + +### Why Ryver? + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/04/Ryver.jpg) + +Why mess with something that works? The developers at Ryver are well aware of Slack, and they’re hoping their improved service will be enough to make you switch over. They promise a completely free team-communication service with no hidden charges along the way. + +Thankfully, they deliver on their main aim with a high quality product. + +Extra content is the name of the game, and they promise to remove some of the limits you’ll find on a free account with Slack. Unlimited data storage is a major plus point, and it’s also more open in a number of ways. If storage limits are an issue for you, you have to check out Ryver. + +It’s a simple system to use, as it was built so that all functions are always one click away. It’s a mantra used to great success by Apple, and there aren’t many growing pains when you first get started. + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/09/ryver-web-interface.png) + +Conversations are split between personal chats and public posts, and it means there’s a clear line between team platforms and personal use. It should help to avoid broadcasting any embarrassing announcements to your colleagues, and I’ve seen a few during my time as a Slack user. + +Integration with a number of existing apps is supported, and there are native applications for most platforms. + +You can add guests when needed at no additional cost, and it’s useful if you deal with external clients regularly. Guests can add more guests, so there’s an element of fluidity that isn’t seen with the more popular option. + +Think of Ryver as a completely different service that will cater to different needs. If you need to deal with numerous clients on the same account, it’s worth trying out. + +The question is how is it free? The quick answer is premium users will be paying your way. Like Spotify and other services, there’s a minority paying for the rest of us. Here’s a direct link to their download page if you’re interested in giving it a go. + +### Should You Switch to Ryver? + +![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2016/04/Slack-homepage.jpg) + +Slack is great as long as you stick to smaller teams like I do, but Ryver has a lot to offer. The idea of a completely free team messaging program is noble, and it works perfectly. + +There’s nothing wrong with using both, so make sure to try out the competition if you’re not willing to pay for a premium Slack account. You might find that both are better in different situations, depending on what you need. + +Above all, Ryver is a great free alternative, and it’s more than just a Slack clone. They have a clear idea of what they’re trying to achieve, and they have a decent product that offers something different in a crowded marketplace. + +However, there’s a chance that it will disappear if there’s a sustained lack of funding in the future. It could leave your teams and discussions in disarray. Everything is fine for now, but be careful if you plan to export a larger business over to the new upstart. + +If you’re tired of Slack’s limitations on a free account, you’ll be impressed by what Ryver has to offer. To learn more, check out their website for information about the service. + +-------------------------------------------------------------------------------- + +via: https://www.maketecheasier.com/why-use-ryver-instead-of-slack/?utm_medium=feed&utm_source=feedpress.me&utm_campaign=Feed%3A+maketecheasier + +作者:[James Milin-Ashmore][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.maketecheasier.com/author/james-ashmore/ From 33ff30721bd778cf04110b1da621de5dfc437aa6 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:22:04 +0800 Subject: [PATCH 34/48] =?UTF-8?q?20160917-21=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...tar command practical examples in Linux.md | 308 ++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 sources/tech/20160911 17 tar command practical examples in Linux.md diff --git a/sources/tech/20160911 17 tar command practical examples in Linux.md b/sources/tech/20160911 17 tar command practical examples in Linux.md new file mode 100644 index 0000000000..d4634ef23c --- /dev/null +++ b/sources/tech/20160911 17 tar command practical examples in Linux.md @@ -0,0 +1,308 @@ +17 tar command practical examples in Linux +===== + +Tar (tape archive ) is the most widely used command in Unix like operating system for creating archive of multiple files and folders into a single archive file and that archive file can be further compressed using gzip and bzip2 techniques. In other words we can say that tar command is used to take backup by archiving multiple files and directory into a single tar or archive file and later on files & directories can be extracted from the tar compressed file. + +In this article we will discuss 17 practical examples of tar command in Linux. + +Syntax of tar command: + +``` +# tar +``` + +Some of the commonly used options in tar command are listed below : + +![](http://www.linuxtechi.com/wp-content/uploads/2016/09/tar-command-options.jpg) + +Note : hyphen ( – ) in the tar command options are optional. + +### Example: 1 Create a tar archive file + +Let’s create a tar file of /etc directory and ‘/root/ anaconda-ks.cfg’ file. + +``` +[root@linuxtechi ~]# tar -cvf myarchive.tar /etc /root/anaconda-ks.cfg +``` + +Above command will create a tar file with the name “myarchive” in the current folder. Tar file contains all the files and directories of /etc folder and anaconda-ks.cfg file. + +In the tar command ‘-c‘ option specify to create a tar file, ‘-v’ is used for verbose output and ‘-f’ option is used to specify the archive file name. + +``` +[root@linuxtechi ~]# ls -l myarchive.tar +-rw-r--r--. 1 root root 22947840 Sep 7 00:24 myarchive.tar +[root@linuxtechi ~]# +``` + +### Example:2 List the contents of tar archive file. + +Using ‘–t‘ option in tar command we can view the contents of tar files without extracting it. + +``` +[root@linuxtechi ~]# tar -tvf myarchive.tar +``` + +Listing a specific file or directory from tar file. In the below example i am trying to view whether ‘anaconda-ks.cfg’ file is there in the tar file or not. + +``` +[root@linuxtechi ~]# tar -tvf myarchive.tar root/anaconda-ks.cfg +-rw------- root/root 953 2016-08-24 01:33 root/anaconda-ks.cfg +[root@linuxtechi ~]# +``` + +### Example:3 Append or add files to end of archive or tar file. + +‘-r‘ option in the tar command is used to append or add file to existing tar file. Let’s add /etc/fstab file in ‘data.tar‘ + +``` +[root@linuxtechi ~]# tar -rvf data.tar /etc/fstab +``` + +Note: In the Compressed tar file we can’t append file or directory. + +### Example:4 Extracting files and directories from tar file. + +‘-x‘ option is used to extract the files and directories from the tar file. Let’s extract the content of above created tar file. + +``` +[root@linuxtechi ~]# tar -xvf myarchive.tar +``` + +This command will extract all the files and directories of myarchive tar file in the current working directory. + +### Example:5 Extracting tar file to a particular folder. + +In case you want to extract tar file to a particular folder or directory then use ‘-C‘ option and after that specify the path of a folder. + +``` +[root@linuxtechi ~]# tar -xvf myarchive.tar -C /tmp/ +``` + +### Example:6 Extracting particular file or directory from tar file. + +Let’s assume you want to extract only anaconda-ks.cfg file from the tar file under /tmp folder. + +Syntax : + +``` +# tar –xvf {tar-file } {file-to-be-extracted } -C {path-where-to-extract} + +[root@linuxtechi tmp]# tar -xvf /root/myarchive.tar root/anaconda-ks.cfg -C /tmp/ +root/anaconda-ks.cfg +[root@linuxtechi tmp]# ls -l /tmp/root/anaconda-ks.cfg +-rw-------. 1 root root 953 Aug 24 01:33 /tmp/root/anaconda-ks.cfg +[root@linuxtechi tmp]# +``` + +### Example:7 Creating and compressing tar file (tar.gz or .tgz ) + +Let’s assume that we want to create a tar file of /etc and /opt folder and also want to compress it using gzip tool. This can be achieved using ‘-z‘ option in tar command. Extensions of such tar files will be either tar.gz or .tgz + +``` +[root@linuxtechi ~]# tar -zcpvf myarchive.tar.gz /etc/ /opt/ +``` + +Or + +``` +[root@linuxtechi ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ +``` + +### Example:8 Creating and compressing tar file ( tar.bz2 or .tbz2 ) + +Let’s assume that we want to create compressed (bzip2) tar file of /etc and /opt folder. This can be achieved by using the option ( -j) in the tar command.Extensions of such tar files will be either tar.bz2 or .tbz + +``` +[root@linuxtechi ~]# tar -jcpvf myarchive.tar.bz2 /etc/ /opt/ +``` + +Or + +``` +[root@linuxtechi ~]# tar -jcpvf myarchive.tbz2 /etc/ /opt/ +``` + +### Example:9 Excluding particular files or type while creating tar file. + +Using “–exclude” option in tar command we can exclude the particular file or file type while creating tar file. Let’s assume we want to exclude the file type of html while creating the compressed tar file. + +``` +[root@linuxtechi ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ --exclude=*.html +``` + +### Example:10 Listing the contents of tar.gz or .tgz file + +Contents of tar file with the extensions tar.gz or .tgz is viewed by using the option ‘-t’. Example is shown below : + +``` +[root@linuxtechi ~]# tar -tvf myarchive.tgz | more +............................................. +drwxr-xr-x root/root 0 2016-09-07 08:41 etc/ +-rw-r--r-- root/root 541 2016-08-24 01:23 etc/fstab +-rw------- root/root 0 2016-08-24 01:23 etc/crypttab +lrwxrwxrwx root/root 0 2016-08-24 01:23 etc/mtab -> /proc/self/mounts +-rw-r--r-- root/root 149 2016-09-07 08:41 etc/resolv.conf +drwxr-xr-x root/root 0 2016-09-06 03:55 etc/pki/ +drwxr-xr-x root/root 0 2016-09-06 03:15 etc/pki/rpm-gpg/ +-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 +-rw-r--r-- root/root 1004 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7 +-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7 +-rw-r--r-- root/root 3140 2015-09-15 06:53 etc/pki/rpm-gpg/RPM-GPG-KEY-foreman +.......................................................... +``` + +### Example:11 Listing the contents of tar.bz2 or .tbz2 file. + +Contents of tar file with the extensions tar.bz2 or .tbz2 is viewed by using the option ‘-t’. Example is shown below : + +``` +[root@linuxtechi ~]# tar -tvf myarchive.tbz2 | more +........................................................ +rwxr-xr-x root/root 0 2016-08-24 01:25 etc/pki/java/ +lrwxrwxrwx root/root 0 2016-08-24 01:25 etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts +drwxr-xr-x root/root 0 2016-09-06 02:54 etc/pki/nssdb/ +-rw-r--r-- root/root 65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db +-rw-r--r-- root/root 9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db +-rw-r--r-- root/root 16384 2010-01-12 16:21 etc/pki/nssdb/key3.db +-rw-r--r-- root/root 11264 2016-09-06 02:54 etc/pki/nssdb/key4.db +-rw-r--r-- root/root 451 2015-10-21 09:42 etc/pki/nssdb/pkcs11.txt +-rw-r--r-- root/root 16384 2010-01-12 15:45 etc/pki/nssdb/secmod.db +drwxr-xr-x root/root 0 2016-08-24 01:26 etc/pki/CA/ +drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/certs/ +drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/crl/ +drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/newcerts/ +drwx------ root/root 0 2015-06-29 08:48 etc/pki/CA/private/ +drwx------ root/root 0 2015-11-20 06:34 etc/pki/rsyslog/ +drwxr-xr-x root/root 0 2016-09-06 03:44 etc/pki/pulp/ +.............................................................. +``` + +### Example:12 Extracting or unzip tar.gz or .tgz files. + +tar files with extension tar.gz or .tgz is extracted or unzipped with option ‘-x’ and ‘-z’. Example is shown below : + +``` +[root@linuxtechi ~]# tar -zxpvf myarchive.tgz -C /tmp/ +``` + +Above command will extract tar file under /tmp folder. + +Note : Now a days tar command will take care compression file types automatically while extracting, it means it is optional for us to specify compression type in tar command. Example is shown below : + +``` +[root@linuxtechi ~]# tar -xpvf myarchive.tgz -C /tmp/ +``` + +### Example:13 Extracting or unzip tar.bz2 or .tbz2 files + +tar files with the extension tar.bz2 or .tbz2 is extract with option ‘-j’ and ‘-x’. Example is shown below: + +``` +[root@linuxtechi ~]# tar -jxpvf myarchive.tbz2 -C /tmp/ +``` + +Or + +``` +[root@linuxtechi ~]# tar xpvf myarchive.tbz2 -C /tmp/ +``` + +### Example:14 Scheduling backup with tar command + +There are some real time scenarios where we have to create the tar files of particular files and directories for backup purpose on daily basis. Let’s suppose we have to take backup of whole /opt folder on daily basis, this can be achieved by creating a cron job of tar command. Example is shown below : + +``` +[root@linuxtechi ~]# tar -zcvf optbackup-$(date +%Y-%m-%d).tgz /opt/ +``` + +Create a cron job for above command. + +### Example:15 Creating compressed archive or tar file with -T and -X option. + +There are some real time scenarios where we want tar command to take input from a file and that file will consists of path of files & directory that are to be archived and compressed, there might some files that we would like to exclude in the archive which are mentioned in input file. + +In the tar command input file is specified after ‘-T’ option and file which consists of exclude list is specified after ‘-X’ option. + +Let’s suppose we want to archive and compress the directories like /etc , /opt and /home and want to exclude the file ‘/etc/sysconfig/kdump’ and ‘/etc/sysconfig/foreman‘, Create a text file ‘/root/tar-include’ and ‘/root/tar-exclude’ and put the following contents in respective file. + +``` +[root@linuxtechi ~]# cat /root/tar-include +/etc +/opt +/home +[root@linuxtechi ~]# +[root@linuxtechi ~]# cat /root/tar-exclude +/etc/sysconfig/kdump +/etc/sysconfig/foreman +[root@linuxtechi ~]# +``` + +Now run the below command to create and compress archive file. + +``` +[root@linuxtechi ~]# tar zcpvf mybackup-$(date +%Y-%m-%d).tgz -T /root/tar-include -X /root/tar-exclude +``` + +### Example:16 View the size of .tar , .tgz and .tbz2 file. + +Use the below commands to view the size of tar and compressed tar files. + +``` +[root@linuxtechi ~]# tar -czf - data.tar | wc -c +427 +[root@linuxtechi ~]# tar -czf - mybackup-2016-09-09.tgz | wc -c +37956009 +[root@linuxtechi ~]# tar -czf - myarchive.tbz2 | wc -c +30835317 +[root@linuxtechi ~]# +``` + +### Example:17 Splitting big tar file into smaller files. + +In Unix like operating big file is divided or split into smaller files using split command. Big tar file can also be divided into the smaller parts using split command. + +Let’s assume we want to split ‘mybackup-2016-09-09.tgz‘ file into smaller parts of each 6 MB. + +``` +Syntax : split -b . “prefix-name” +``` + +``` +[root@linuxtechi ~]# split -b 6M mybackup-2016-09-09.tgz mybackup-parts +``` + +Above command will split the mybackup compressed tar file into the smaller files each of size 6 MB in current working directory and split file names will starts from mybackup-partsaa … mybackup-partsag. In case if you want to append numbers in place of alphabets then use ‘-d’ option in above split command. + +``` +[root@linuxtechi ~]# ls -l mybackup-parts* +-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaa +-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsab +-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsac +-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsad +-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsae +-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaf +-rw-r--r--. 1 root root 637219 Sep 10 03:05 mybackup-partsag +[root@linuxtechi ~]# +``` + +Now we can move these files into another server over the network and then we can merge all the files into a single tar compressed file using below mentioned command. + +``` +[root@linuxtechi ~]# cat mybackup-partsa* > mybackup-2016-09-09.tgz +[root@linuxtechi ~]# +``` + +That’s all, hope you like different examples of tar command. Please share your feedback and comments. + +-------------------------------------------------------------------------------- + +via: http://www.linuxtechi.com/17-tar-command-examples-in-linux/ + +作者:[Pradeep Kumar ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.linuxtechi.com/author/pradeep/ From 4b9c568f8ebcc4dcfae70b88d68d38b22b10ad09 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:24:28 +0800 Subject: [PATCH 35/48] =?UTF-8?q?20160917-22=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...adership advice for starting a new role.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sources/talk/20160912 Adobe's new CIO shares leadership advice for starting a new role.md diff --git a/sources/talk/20160912 Adobe's new CIO shares leadership advice for starting a new role.md b/sources/talk/20160912 Adobe's new CIO shares leadership advice for starting a new role.md new file mode 100644 index 0000000000..a4a962e8d2 --- /dev/null +++ b/sources/talk/20160912 Adobe's new CIO shares leadership advice for starting a new role.md @@ -0,0 +1,50 @@ +Adobe's new CIO shares leadership advice for starting a new role +==== + +![](https://enterprisersproject.com/sites/default/files/styles/620x350/public/images/CIO_Leadership_3.png?itok=QWUGMw-V) + +I’m currently a few months into a new CIO role at a highly-admired, cloud-based technology company. One of my first tasks was to get to know the organization’s people, culture, and priorities. + +As part of that goal, I am visiting all the major IT sites. While In India, less than two months into the job, I was asked directly: “What are you going to do? What is your plan?” My response, which will not surprise seasoned CIOs, was that I was still in discovery mode, and I was there to listen and learn. + +I’ve never gone into an organization with a set blueprint for what I’ll do. I know some CIOs have a playbook for how they will operate. They’ll come in and blow the whole organization up and put their set plan in motion. + +Yes, there may be situations where things are massively broken and not working, so that course of action makes sense. Once I’m inside a company, however, my strategy is to go through a discovery process. I don’t want to have any preconceived notions about the way things should be or what’s working versus what’s not. + +Here are my guiding principles as a newly-appointed leader: + +### Get to know your people + +This means building relationships, and it includes your IT staff as well as your business users and your top salespeople. What are the top things on their lists? What do they want you to focus on? What’s working well? What’s not? How is the customer experience? Knowing how you can help everyone be more successful will help you shape the way you deliver services to them. + +If your department is spread across several floors, as mine is, consider meet-and-greet lunches or mini-tech fairs so people can introduce themselves, discuss what they’re working on, and share stories about their family, if they feel comfortable doing that. If you have an open-door office policy, make sure they know that as well. If your staff spreads across countries or continents, get out there and visit as soon as you reasonably can. + +### Get to know your products and company culture + +One of the things that surprised me coming into to Adobe was how broad our product portfolio is. We have a platform of solutions and services across three clouds – Adobe Creative Cloud, Document Cloud and Marketing Cloud – and a vast portfolio of products within each. You’ll never know how much opportunity your new company presents until you get to know your products and learn how to support all of them. At Adobe we use many of our digital media and digital marketing solutions as Customer Zero, so we have first-hand experiences to share with our customers + +### Get to know customers + +Very early on, I started getting requests to meet with customers. Meeting with customers is a great way to jump-start your thinking into the future of the IT organization, which includes the different types of technologies, customers, and consumers we could have going forward. + +### Plan for the future + +As a new leader, I have a fresh perspective and can think about the future of the organization without getting distracted by challenges or obstacles. + +What CIOs need to do is jump-start IT into its next generation. When I meet my staff, I’m asking them what we want to be three to five years out so we can start positioning ourselves for that future. That means discussing the initiatives and priorities. + +After that, it makes sense to bring the leadership team together so you can work to co-create the next generation of the organization – its mission, vision, modes of alignment, and operating norms. If you start changing IT from the inside out, it will percolate into business and everything else you do. + +Through this whole process, I’ve been very open with people that this is not going to be a top-down directive. I have ideas on priorities and what we need to focus on, but we have to be in lockstep, working as a team and figuring out what we want to do jointly. + +-------------------------------------------------------------------------------- + +via: https://enterprisersproject.com/article/2016/9/adobes-new-cio-shares-leadership-advice-starting-new-role + +作者:[Cynthia Stoddard][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://enterprisersproject.com/user/cynthia-stoddard From ef05f3622569e25b1abd3260c29d3ea4890b264c Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:31:49 +0800 Subject: [PATCH 36/48] =?UTF-8?q?20160917-23=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...en Source Artificial Intelligence Tools.md | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 sources/tech/20160912 15 Top Open Source Artificial Intelligence Tools.md diff --git a/sources/tech/20160912 15 Top Open Source Artificial Intelligence Tools.md b/sources/tech/20160912 15 Top Open Source Artificial Intelligence Tools.md new file mode 100644 index 0000000000..56f78f84b9 --- /dev/null +++ b/sources/tech/20160912 15 Top Open Source Artificial Intelligence Tools.md @@ -0,0 +1,125 @@ +15 Top Open Source Artificial Intelligence Tools +==== + +Artificial Intelligence (AI) is one of the hottest areas of technology research. Companies like IBM, Google, Microsoft, Facebook and Amazon are investing heavily in their own R&D, as well as buying up startups that have made progress in areas like machine learning, neural networks, natural language and image processing. Given the level of interest, it should come as no surprise that a recent [artificial intelligence report][1] from experts at Stanford University concluded that "increasingly useful applications of AI, with potentially profound positive impacts on our society and economy are likely to emerge between now and 2030." + +In a recent [article][2], we provided an overview of 45 AI projects that seem particularly promising or interesting. In this slideshow, we're focusing in on open source artificial intelligence tools, with a closer look at fifteen of the best-known open source AI projects. + +![](http://www.datamation.com/imagesvr_ce/5668/00AI.jpg) + +Open Source Artificial Intelligence + +These open source AI applications are on the cutting edge of artificial intelligence research. + +![](http://www.datamation.com/imagesvr_ce/8922/01Caffe.JPG) + +### 1. Caffe + +The brainchild of a UC Berkeley PhD candidate, Caffe is a deep learning framework based on expressive architecture and extensible code. It's claim to fame is its speed, which makes it popular with both researchers and enterprise users. According to its website, it can process more than 60 million images in a single day using just one NVIDIA K40 GPU. It is managed by the Berkeley Vision and Learning Center (BVLC), and companies like NVIDIA and Amazon have made grants to support its development. + +![](http://www.datamation.com/imagesvr_ce/1232/02CNTK.JPG) + +### 2. CNTK + +Short for Computational Network Toolkit, CNTK is one of Microsoft's open source artificial intelligence tools. It boasts outstanding performance whether it is running on a system with only CPUs, a single GPU, multiple GPUs or multiple machines with multiple GPUs. Microsoft has primarily utilized it for research into speech recognition, but it is also useful for applications like machine translation, image recognition, image captioning, text processing, language understanding and language modeling. + +![](http://www.datamation.com/imagesvr_ce/2901/03Deeplearning4j.JPG) + +### 3. Deeplearning4j + +Deeplearning4j is an open source deep learning library for the Java Virtual Machine (JVM). It runs in distributed environments and integrates with both Hadoop and Apache Spark. It makes it possible to configure deep neural networks, and it's compatible with Java, Scala and other JVM languages. + +The project is managed by a commercial company called Skymind, which offers paid support, training and an enterprise distribution of Deeplearning4j. + +![](http://www.datamation.com/imagesvr_ce/7269/04DMLT.JPG) + +### 4. Distributed Machine Learning Toolkit + +Like CNTK, the Distributed Machine Learning Toolkit (DMTK) is one of Microsoft's open source artificial intelligence tools. Designed for use in big data applications, it aims to make it faster to train AI systems. It consists of three key components: the DMTK framework, the LightLDA topic model algorithm, and the Distributed (Multisense) Word Embedding algorithm. As proof of DMTK's speed, Microsoft says that on an eight-cluster machine, it can "train a topic model with 1 million topics and a 10-million-word vocabulary (for a total of 10 trillion parameters), on a document collection with over 100-billion tokens," a feat that is unparalleled by other tools. + +![](http://www.datamation.com/imagesvr_ce/2890/05H2O.JPG) + +### 5. H20 + +Focused more on enterprise uses for AI than on research, H2O has large companies like Capital One, Cisco, Nielsen Catalina, PayPal and Transamerica among its users. It claims to make is possible for anyone to use the power of machine learning and predictive analytics to solve business problems. It can be used for predictive modeling, risk and fraud analysis, insurance analytics, advertising technology, healthcare and customer intelligence. + +It comes in two open source versions: standard H2O and Sparkling Water, which is integrated with Apache Spark. Paid enterprise support is also available. + +![](http://www.datamation.com/imagesvr_ce/1127/06Mahout.JPG) + +### 6. Mahout + +An Apache Foundation project, Mahout is an open source machine learning framework. According to its website, it offers three major features: a programming environment for building scalable algorithms, premade algorithms for tools like Spark and H2O, and a vector-math experimentation environment called Samsara. Companies using Mahout include Adobe, Accenture, Foursquare, Intel, LinkedIn, Twitter, Yahoo and many others. Professional support is available through third parties listed on the website. + +![](http://www.datamation.com/imagesvr_ce/4038/07MLlib.JPG) + +### 7. MLlib + +Known for its speed, Apache Spark has become one of the most popular tools for big data processing. MLlib is Spark's scalable machine learning library. It integrates with Hadoop and interoperates with both NumPy and R. It includes a host of machine learning algorithms for classification, regression, decision trees, recommendation, clustering, topic modeling, feature transformations, model evaluation, ML pipeline construction, ML persistence, survival analysis, frequent itemset and sequential pattern mining, distributed linear algebra and statistics. + +![](http://www.datamation.com/imagesvr_ce/839/08NuPIC.JPG) + +### 8. NuPIC + +Managed by a company called Numenta, NuPIC is an open source artificial intelligence project based on a theory called Hierarchical Temporal Memory, or HTM. Essentially, HTM is an attempt to create a computer system modeled after the human neocortex. The goal is to create machines that "approach or exceed human level performance for many cognitive tasks." + +In addition to the open source license, Numenta also offers NuPic under a commercial license, and it also offers licenses on the patents that underlie the technology. + +![](http://www.datamation.com/imagesvr_ce/99/09OpenNN.JPG) + +### 9. OpenNN + +Designed for researchers and developers with advanced understanding of artificial intelligence, OpenNN is a C++ programming library for implementing neural networks. Its key features include deep architectures and fast performance. Extensive documentation is available on the website, including an introductory tutorial that explains the basics of neural networks. Paid support for OpenNNis available through Artelnics, a Spain-based firm that specializes in predictive analytics. + +![](http://www.datamation.com/imagesvr_ce/4168/10OpenCyc.JPG) + +### 10. OpenCyc + +Developed by a company called Cycorp, OpenCyc provides access to the Cyc knowledge base and commonsense reasoning engine. It includes more than 239,000 terms, about 2,093,000 triples, and about 69,000 owl:sameAs links to external semantic data namespaces. It is useful for rich domain modeling, semantic data integration, text understanding, domain-specific expert systems and game AIs. The company also offers two other versions of Cyc: one for researchers that is free but not open source and one for enterprise use that requires a fee. + +![](http://www.datamation.com/imagesvr_ce/9761/11Oryx2.JPG) + +### 11. Oryx 2 + +Built on top of Apache Spark and Kafka, Oryx 2 is a specialized application development framework for large-scale machine learning. It utilizes a unique lambda architecture with three tiers. Developers can use Oryx 2 to create new applications, and it also includes some pre-built applications for common big data tasks like collaborative filtering, classification, regression and clustering. The big data tool vendor Cloudera created the original Oryx 1 project and has been heavily involved in continuing development. + +![](http://www.datamation.com/imagesvr_ce/7423/12.%20PredictionIO.JPG) + +### 12. PredictionIO + +In February this year, Salesforce bought PredictionIO, and then in July, it contributed the platform and its trademark to the Apache Foundation, which accepted it as an incubator project. So while Salesforce is using PredictionIO technology to advance its own machine learning capabilities, work will also continue on the open source version. It helps users create predictive engines with machine learning capabilities that can be used to deploy Web services that respond to dynamic queries in real time. + +![](http://www.datamation.com/imagesvr_ce/6886/13SystemML.JPG) + +### 13. SystemML + +First developed by IBM, SystemML is now an Apache big data project. It offers a highly-scalable platform that can implement high-level math and algorithms written in R or a Python-like syntax. Enterprises are already using it to track customer service on auto repairs, to direct airport traffic and to link social media data with banking customers. It can run on top of Spark or Hadoop. + +![](http://www.datamation.com/imagesvr_ce/5742/14TensorFlow.JPG) + +### 14. TensorFlow + +TensorFlow is one of Google's open source artificial intelligence tools. It offers a library for numerical computation using data flow graphs. It can run on a wide variety of different systems with single- or multi-CPUs and GPUs and even runs on mobile devices. It boasts deep flexibility, true portability, automatic differential capabilities and support for Python and C++. The website includes a very extensive list of tutorials and how-tos for developers or researchers interested in using or extending its capabilities. + +![](http://www.datamation.com/imagesvr_ce/9018/15Torch.JPG) + +### 15. Torch + +Torch describes itself as "a scientific computing framework with wide support for machine learning algorithms that puts GPUs first." The emphasis here is on flexibility and speed. In addition, it's fairly easy to use with packages for machine learning, computer vision, signal processing, parallel processing, image, video, audio and networking. It relies on a scripting language called LuaJIT that is based on Lua. + + + + +-------------------------------------------------------------------------------- + +via: http://www.datamation.com/open-source/slideshows/15-top-open-source-artificial-intelligence-tools.html + +作者:[Cynthia Harvey][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.datamation.com/author/Cynthia-Harvey-6460.html +[1]: https://ai100.stanford.edu/sites/default/files/ai_100_report_0906fnlc_single.pdf +[2]: http://www.datamation.com/applications/artificial-intelligence-software-45-ai-projects-to-watch-1.html From 949d1414273a67d50becd14789851dbf80c4c83e Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:34:47 +0800 Subject: [PATCH 37/48] =?UTF-8?q?20160917-24=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...illiant Command-Line TODO App For Linux.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 sources/tech/20160913 Taskwarrior: A Brilliant Command-Line TODO App For Linux.md diff --git a/sources/tech/20160913 Taskwarrior: A Brilliant Command-Line TODO App For Linux.md b/sources/tech/20160913 Taskwarrior: A Brilliant Command-Line TODO App For Linux.md new file mode 100644 index 0000000000..6b44101e53 --- /dev/null +++ b/sources/tech/20160913 Taskwarrior: A Brilliant Command-Line TODO App For Linux.md @@ -0,0 +1,52 @@ +Taskwarrior: A Brilliant Command-Line TODO App For Linux +==== + +Taskwarrior is a simple, straight-forward command-line based TODO app for Ubuntu/Linux. This open-source app has to be one of the easiest of all [CLI based apps][4] I've ever used. Taskwarrior helps you better organize yourself, and without installing bulky new apps which sometimes defeats the whole purpose of TODO apps. + +![](https://2.bp.blogspot.com/-pQnRlOUNIxk/V9cuc3ytsBI/AAAAAAAAKHs/yYxyiAk4PwMIE0HTxlrm6arWOAPcBRRywCLcB/s1600/taskwarrior-todo-app.png) + +### Taskwarrior: A Simple CLI Based TODO App That Gets The Job Done! + +Taskwarrior is an open-source and cross-platform, command-line based TODO app, which lets you manage your to-do lists right from the Terminal. The app lets you add tasks, shows you the list, and removes tasks from that list with much ease. And what's more, it's available within your default repositories, no need to fiddle with PPAs. In Ubuntu 16.04 LTS and similar, do the following in Terminal to install Taskwarrior. + +``` +sudo apt-get install task +``` + +A simple use case can be as follows: + +``` +$ task add Read a book +Created task 1. +$ task add priority:H Pay the bills +Created task 2. +``` + +This is the same example I used in the screenshot above. Yes, you can set priority levels (H, L or M) as shown. And then you can use 'task' or 'task next' commands to see your newly-created todo list. For example: + +``` +$ task next + +ID Age P Description Urg +-- --- - -------------------------------- ---- + 2 10s H Pay the bills 6 + 1 20s Read a book 0 +``` + +And once its completed, you can use 'task 1 done' or 'task 2 done' commands to clear the lists. A more comprehensive list of commands, use-cases [can be found here][1]. Also, Taskwarrior is cross-platform, which means you'll find a version that [fits your needs][2] no matter what. There's even an [Android version][3] if you want one. Enjoy! + +-------------------------------------------------------------------------------- + +via: http://www.techdrivein.com/2016/09/taskwarrior-command-line-todo-app-linux.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+techdrivein+%28Tech+Drive-in%29 + +作者:[Manuel Jose ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.techdrivein.com/2016/09/taskwarrior-command-line-todo-app-linux.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+techdrivein+%28Tech+Drive-in%29 +[1]: https://taskwarrior.org/docs/ +[2]: https://taskwarrior.org/download/ +[3]: https://taskwarrior.org/news/news.20160225.html +[4]: http://www.techdrivein.com/search/label/Terminal From 2bf34b32bd49cc4dc48800b99b030577fd337c1c Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:42:45 +0800 Subject: [PATCH 38/48] =?UTF-8?q?20160917-25=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...and dirty with Windows Nano Server 2016.md | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 sources/tech/20160914 Down and dirty with Windows Nano Server 2016.md diff --git a/sources/tech/20160914 Down and dirty with Windows Nano Server 2016.md b/sources/tech/20160914 Down and dirty with Windows Nano Server 2016.md new file mode 100644 index 0000000000..613d3de86e --- /dev/null +++ b/sources/tech/20160914 Down and dirty with Windows Nano Server 2016.md @@ -0,0 +1,85 @@ +Down and dirty with Windows Nano Server 2016 +==== + +![](http://images.techhive.com/images/article/2016/04/pokes-fun-at-1164459_1280-100654917-primary.idge.jpg) + +>Nano Server is a very fast, powerful tool for remotely administering Windows servers, but you need to know what you're doing + +There's been a good deal of talk around the [upcoming Nano version of Windows Server 2016][1], the remote-administered, command-line version designed with private clouds and datacenters in mind. But there's also a big difference between talking about it and getting your hands into it. Let's get into the guts. + +Nano has no local login, is 64-bit all the way (applications, tools, and agents), and is fast to set up, update, and restart (for the rare times it needs to restart). It's perfect for compute hosts in or out of a cluster, a storage host, a DNS server, an IIS web server, and any server-hosting applications running in a container or virtual-machine guest operating system. + +A Nano Server isn't all that fun to play with: You have to know what you want to accomplish. Otherwise, you'll be looking at a remote PowerShell connection and wondering what you're supposed to do next. But if you know what you want, it's very fast and powerful. + +Microsoft has provided a [quick-start guide][2] to setting up Nano Server. Here, I take the boots-on-the-ground approach to show you what it's like in the real world. + +First, you have to create a .vhd virtual hard drive file. As you can see in Figure 1, I had a few issues with files not being in the right place. PowerShell errors often indicate a mistyped line, but in this case, I had to keep double-checking where I put the files so that it could use the ISO information (which has to be copied and pasted to the server you want to create the .vhd file on). Once you have everything in place, you should see it go through the process of creating the .vhd file. + +![](http://images.techhive.com/images/article/2016/09/nano-server-1-100682371-large.idge.jpg) +>Figure 1: One of the many file path errors I got when trying to run the New-NanoServerImage script. Once I worked out the file-location issues, it went through and created the .vhd file (as shown here). + +Next, when you create the VM in Hyper-V using the VM wizard, you need to point to an existing virtual hard disk and point to the new .vhd file you created (Figure 2). + +![](http://images.techhive.com/images/article/2016/09/nano-server-2-100682368-large.idge.jpg) +>Figure 2: Connecting to a virtual hard disk (the one you created at the start). + +When you start up the Nano server, you may get a memory error depending on how much memory you allocated and how much memory the Hyper-V server has left if you have other VMs running. I had to shut off a few VMs and increase the RAM until it finally started up. That was unexpected -- [Microsoft's Nano system][3] requirements say you can run it with 512MB, although it recommends you give it at least 800MB. (I ended up allocating 8GB after 1GB didn't work; I was impatient, so I didn't try increments in between.) + +I finally came to the login screen, then signed in to get the Nano Server Recovery Console (Figure 3), which is essentially Nano server's terminal screen. + +![](http://images.techhive.com/images/article/2016/09/nano-server-3-100682369-large.idge.jpg) +>Figure 3: The Nano Server Recovery Console. + +Once I was in, I thought I was golden. But in trying to figure out a few details (how to join a domain, how to inject drivers I might not have, how to add roles), I realized that some configuration pieces would have been easier to add when I ran the New-NanoServerImage cmdlet by popping in a few more parameters. + +However, once you have the server up and running, there are ways to configure it live. It all starts with a Remote PowerShell connection, as Figure 4 shows. + +![](http://images.techhive.com/images/article/2016/09/nano-server-4-100682370-large.idge.jpg) +>Figure 4: Getting information from the Nano Server Recovery Console that you can use to perform a PowerShell Remote connection. + +Microsoft provides direction on how to make the connection happen, but after trying four different sites, I found MSDN has the clearest (working) direction on the subject. Figure 5 shows the result. + +![](http://images.techhive.com/images/article/2016/09/nano-server-5-100682372-large.idge.jpg) +>Figure 5: Making the remote PowerShell connection to your Nano Server. + +Note: Once you've done the remote connection the long way, you can connect more quickly using a single line: + +``` +Enter-PSSession –ComputerName "192.168.0.100"-Credential ~\Administrator. +``` + +If you knew ahead of time that this server was going to be a DNS server or be part of a compute cluster and so on, you would have added those roles or feature packages when you were creating the .vhd image in the first place. If you're looking to do so after the fact, you'll need to make the remote PowerShell connection, then install the NanoServerPackage and import it. Then you can see which packages you want to deploy using Find-NanoServerPackage (shown in Figure 6). + +![](http://images.techhive.com/images/article/2016/09/nano-server-6-100682373-large.idge.jpg) +>Figure 6: Once you have installed and imported the NanoServerPackage, you can find the one you need to get your Nano Server up and running with the roles and features you require. + +I tested this out by running the DNS package with the following command: `Install-NanoServerPackage –Name Microsoft-NanoServer-DNS-Package`. Once it was installed, I had to enable it with the following command: `Enable-WindowsOptionalFeature –Online –FeatureName DNS-Server-Full-Role`. + +Obviously I didn't know these commands ahead of time. I have never run them before in my life, nor had I ever enabled a DNS role this way, but with a little research I had a DNS (Nano) Server up and running. + +The next part of the process involves using PowerShell to configure the DNS server. That's a completely different topic and one best researched online. But it doesn't appear to be mind-blowingly difficult once you've learned the cmdlets to use: Add a zone? Use the Add-DNSServerPrimaryZone cmdlet. Add a record in that zone? Use the Add-DNSServerResourceRecordA. And so on. + +After doing all this command-line work, you'll likely want proof that any of this is working. You should be able to do a quick review of PowerShell commands and not the many DNS ones that now present themselves (using Get-Command). + +But if you need a GUI-based confirmation, you can open Server Manager on a GUI-based server and add the IP address of the Nano Server. Then right-click that server and choose Manage As to provide your credentials (~\Administrator and password). Once you have connected, right-click the server in Server Manager and choose Add Roles and Features; it should show that you have DNS installed as a role, as Figure 7 shows. + +![](http://images.techhive.com/images/article/2016/09/nano-server-7-100682374-large.idge.jpg) +>Figure 7: Proving through the GUI that DNS was installed. + +Don't bother trying to remote-desktop into the server. There is only so much you can do through the Server Manager tool, and that isn't one of them. And just because you can confirm the DNS role doesn't mean you have the ability to add new roles and features through the GUI. It's all locked down. Nano Server is how you'll make any needed adjustments. + +-------------------------------------------------------------------------------- + +via: http://www.infoworld.com/article/3119770/windows-server/down-and-dirty-with-windows-nano-server-2016.html?utm_source=webopsweekly&utm_medium=email + +作者:[J. Peter Bruzzese ][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.infoworld.com/author/J.-Peter-Bruzzese/ +[1]: http://www.infoworld.com/article/3049191/windows-server/nano-server-a-slimmer-slicker-windows-server-core.html +[2]: https://technet.microsoft.com/en-us/windows-server-docs/compute/nano-server/getting-started-with-nano-server +[3]: https://technet.microsoft.com/en-us/windows-server-docs/get-started/system-requirements--and-installation + From 5254daae23ca68ac6ad982192b163f3f330cbe21 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:47:31 +0800 Subject: [PATCH 39/48] =?UTF-8?q?20160917-26=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... Principles of Monitoring Microservices.md | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 sources/tech/20160913 The Five Principles of Monitoring Microservices.md diff --git a/sources/tech/20160913 The Five Principles of Monitoring Microservices.md b/sources/tech/20160913 The Five Principles of Monitoring Microservices.md new file mode 100644 index 0000000000..9d283f8bdf --- /dev/null +++ b/sources/tech/20160913 The Five Principles of Monitoring Microservices.md @@ -0,0 +1,95 @@ +The Five Principles of Monitoring Microservices +==== + +![](http://thenewstack.io/wp-content/uploads/2016/09/toppicsysdig.jpg) + +The need for microservices can be summed up in just one word: speed. The need to deliver more functionality and reliability faster has revolutionized the way developers create software. Not surprisingly, this change has caused ripple effects within software management, including monitoring systems. In this post, we’ll focus on the radical changes required to monitor your microservices in production efficiently. We’ll lay out five guiding principles for adapting your monitoring approach for this new software architecture. + +Monitoring is a critical piece of the control systems of microservices, as the more complex your software gets, the harder it is to understand its performance and troubleshoot problems. Given the dramatic changes to software delivery, however, monitoring needs an overhaul to perform well in a microservice environment. The rest of this article presents the five principles of monitoring microservices, as follows: + +1. Monitor containers and what’s inside them. +2. Alert on service performance, not container performance. +3. Monitor services that are elastic and multi-location. +4. Monitor APIs. +5. Map your monitoring to your organizational structure. + +Leveraging these five principles will allow you to establish more effective monitoring as you make your way towards microservices. These principles will allow you to address both the technological changes associated with microservices, in addition to the organizational changes related to them. + +### The Principles of Microservice Monitoring + +#### 1. Monitor Containers and What’s Running Inside Them + +Containers gained prominence as the building blocks of microservices. The speed, portability, and isolation of containers made it easy for developers to embrace a microservice model. There’s been a lot written on the benefits of containers so we won’t recount it all here. + +Containers are black boxes to most systems that live around them. That’s incredibly useful for development, enabling a high level of portability from development through production, from developer laptop to cloud. But when it comes to operating, monitoring and troubleshooting a service, black boxes make common activities harder, leading us to wonder: what’s running in the container? How is the application/code performing? Is it spitting out important custom metrics? From the DevOps perspective, you need deep visibility inside containers rather than just knowing that some containers exist. + +![](http://thenewstack.io/wp-content/uploads/2016/09/greatfordev.jpg) + +The typical process for instrumentation in a non-containerized environment — an agent that lives in the user space of a host or VM — doesn’t work particularly well for containers. That’s because containers benefit from being small, isolated processes with as few dependencies as possible. + +And, at scale, running thousands of monitoring agents for even a modestly-sized deployment is an expensive use of resources and an orchestration nightmare. Two potential solutions arise for containers: 1) ask your developers to instrument their code directly, or 2) leverage a universal kernel-level instrumentation approach to see all application and container activity on your hosts. We won’t go into depth here, but each method has pros and cons. + +#### 2. Leverage Orchestration Systems to Alert on Service Performance + +Making sense of operational data in a containerized environment is a new challenge. The metrics of a single container have a much lower marginal value than the aggregate information from all the containers that make up a function or a service. + +This particularly applies to application-level information, like which queries have the slowest response times or which URLs are seeing the most errors, but also applies to infrastructure-level monitoring, like which services’ containers are using the most resources beyond their allocated CPU shares. + +Increasingly, software deployment requires an orchestration system to “translate” a logical application blueprint into physical containers. Common orchestration systems include Kubernetes, Mesosphere DC/OS and Docker Swarm. Teams use an orchestration system to (1) define your microservices and (2) understand the current state of each service in deployment. You could argue that the orchestration system is even more important than the containers. The actual containers are ephemeral — they matter only for the short time that they exist — while your services matter for the life of their usefulness. + +DevOps teams should redefine alerts to focus on characteristics that get as close to monitoring the experience of the service as possible. These alerts are the first line of defense in assessing if something is impacting the application. But getting to these alerts is challenging, if not impossible unless your monitoring system is container-native. + +Container-native solutions leverage orchestration metadata to dynamically aggregate container and application data and calculate monitoring metrics on a per-service basis. Depending on your orchestration tool, you might have different layers of a hierarchy that you’d like to drill into. For example, in Kubernetes, you typically have a Namespace, ReplicaSets, Pods and some containers. Aggregating at these various layers is essential for logical troubleshooting, regardless of the physical deployment of the containers that make up the service. + +![](http://thenewstack.io/wp-content/uploads/2016/09/servicemonitoring.jpg) + +#### 3. Be Prepared for Services that are Elastic and Multi-Location + +Elastic services are certainly not a new concept, but the velocity of change is much faster in container-native environments than virtualized environments. Rapidly changing environments can wreak havoc on brittle monitoring systems. + +Frequently monitoring legacy systems required manual tuning of metrics and checks based on individual deployments of software. This tuning can be as specific as defining the individual metrics to be captured, or configuring collection based on what application is operating in a particular container. While that may be acceptable on a small scale (think tens of containers), it would be unbearable in anything larger. Microservice focused monitoring must be able to comfortably grow and shrink in step with elastic services, without human intervention. + +For example, if the DevOps team must manually define what service a container is included in for monitoring purposes, they no doubt drop the ball as Kubernetes or Mesos spins up new containers regularly throughout the day. Similarly, if Ops were required to install a custom stats endpoint when new code is built and pushed into production, challenges may arise as developers pull base images from a Docker registry. + +In production, build monitoring toward a sophisticated deployment that spans multiple data centers or multiple clouds. Leveraging, for example, AWS CloudWatch will only get you so far if your services span your private data center as well as AWS. That leads back to implementing a monitoring system that can span these different locations as well as operate in dynamic, container-native environments. + +#### 4. Monitor APIs + +In microservice environments, APIs are the lingua franca. They are essentially the only elements of a service that are exposed to other teams. In fact, response and consistency of the API may be the “internal SLA” even if there isn’t a formal SLA defined. + +As a result, API monitoring is essential. API monitoring can take many forms but clearly, must go beyond binary up/down checks. For instance, it’s valuable to understand the most frequently used endpoints as a function of time. This allows teams to see if anything noticeable has changed in the usage of services, whether it be due to a design change or a user change. + +You can also consider the slowest endpoints of your service, as these can reveal significant problems, or, at the very least, point to areas that need the most optimization in your system. + +Finally, the ability to trace service calls through your system represents another critical capability. While typically used by developers, this type of profiling will help you understand the overall user experience while breaking information down into infrastructure and application-based views of your environment. + +#### 5. Map Monitoring to Your Organizational Structure + +While most of this post has been focused on the technological shift in microservices and monitoring, like any technology story, this is as much about people as it is about software bits. + +For those of you familiar with Conway’s law, he reminds us that the design of systems is defined by the organizational structure of the teams building them. The allure of creating faster, more agile software has pushed teams to think about restructuring their development organization and the rules that govern it. + +![](http://thenewstack.io/wp-content/uploads/2016/09/mapmonitoring.jpg) + +So if an organization wants to benefit from this new software architecture approach, their teams must, therefore, mirror microservices themselves. That means smaller teams, loosely coupled; that can choose their direction as long as it still meets the needs of the whole. Within each team, there is more control than ever over languages used, how bugs are handled, or even operational responsibilities. + +DevOps teams can enable a monitoring platform that does exactly this: allows each microservice team to isolate their alerts, metrics, and dashboards, while still giving operations a view into the global system. + +### Conclusion + +There’s one, clear trigger event that precipitated the move to microservices: speed. Organizations wanted to deliver more capabilities to their customers in less time. Once this happened, technology stepped in, the architectural move to micro-services and the underlying shift to containers make speed happen. Anything that gets in the way of this progress train is going to get run over on the tracks. + +As a result, the fundamental principles of monitoring need to adapt to the underlying technology and organizational changes that accompany microservices. Operations teams that recognize this shift can adapt to microservices earlier and easier. + +-------------------------------------------------------------------------------- + +via: http://linoxide.com/firewall/pfsense-setup-basic-configuration/ + +作者:[Apurva Dave][a] [Loris Degioanni][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://thenewstack.io/author/apurvadave/ +[b]: http://thenewstack.io/author/lorisdegioanni/ From e13ed6e31cd21d3a9c563c2622c150835ef72bc2 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 22:55:11 +0800 Subject: [PATCH 40/48] =?UTF-8?q?20160917-27=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...tainers with Elasticsearch and cAdvisor.md | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 sources/tech/20160913 Monitoring Docker Containers with Elasticsearch and cAdvisor.md diff --git a/sources/tech/20160913 Monitoring Docker Containers with Elasticsearch and cAdvisor.md b/sources/tech/20160913 Monitoring Docker Containers with Elasticsearch and cAdvisor.md new file mode 100644 index 0000000000..fe5d8025a7 --- /dev/null +++ b/sources/tech/20160913 Monitoring Docker Containers with Elasticsearch and cAdvisor.md @@ -0,0 +1,207 @@ +Monitoring Docker Containers with Elasticsearch and cAdvisor +======= + +If you’re running a Swarm Mode cluster or even a single Docker engine, you’ll end up asking this question: + +>How do I keep track of all that’s happening? + +The answer is “not easily.” + +You need a few things to have a complete overview of stuff like: + + +1. Number and status of containers +2. If, where, and when a container has been moved to another node +3. Number of containers on a given node +4. Traffic peaks at a given time +5. Orphan volumes and networks +6. Free disk space, free inodes +7. Number of containers against number of veths attached to the docker0 and docker_gwbridge bridges +8. Up and down Swarm nodes +9. Centralize logs + +The goal of this post is to demonstrate the use of [Elasticsearch][1] + [Kibana][2] + [cAdvisor][3] as tools to analyze and gather metrics and visualize dashboards for Docker containers. + +Later on in this post, you can find a dashboard trying to address a few points from the previous list. There are also points that can’t be addressed by simply using cAdvisor, like the status of Swarm Mode nodes. + +Also, if you have specific needs that aren’t covered by cAdvisor or another tool, I encourage you to write your own data collector and data shipper (e.g., [Beats][4]). Note that I won’t be showing you how to centralize Docker containers log on Elasticsearch. + +>[“How do you keep track of all that’s happening in a Swarm Mode cluster? Not easily.” via @fntlnz][5] + +### Why Do We Need to Monitor Containers? + +Imagine yourself in the classic situation of managing a virtual machine, either just one or several. You are a tmux hero, so you have your sessions preconfigured to do basically everything, monitoring included. There’s a problem in production? You just do a top, htop, iotop, jnettop, whatevertop on all your machines, and you’re ready for troubleshooting! + +Now imagine that you have the same three nodes but split into 50 containers. You need some history displayed nicely in a single place where you can perform queries to know what happened instead of just risking your life in front of those ncurses tools. + +### What Is the Elastic Stack? + +The Elastic Stack is a set of tools composed of: + +- Elasticsearch +- Kibana +- Logstash +- Beats + +We’re going to use a few open-source tools from the Elastic Stack, such as Elasticsearch for the JSON-based analytics engine and Kibana to visualize data and create dashboards. + +Another important piece of the Elastic Stack is Beats, but in this post, we’re focused on containers. There’s no official Beat for Docker, so we’ll just use cAdvisor that can natively talk with Elasticsearch. + +cAdvisor is a tool that collects, aggregates, and exports metrics about running containers. In our case, those metrics are being exported to an Elasticsearch storage. + +Two cool facts about cAdvisor are: + +- It’s not limited to Docker containers. +- It has its own webserver with a simple dashboard to visualize gathered metrics for the current node. + +### Set Up a Test Cluster or BYOI + +As I did in my previous posts, my habit is to provide a small script to allow the reader to set up a test environment on which to try out my project’s steps in no time. So you can use the following not-for-production-use script to set up a little Swarm Mode cluster with Elasticsearch running as a container. + +>If you have enough time/experience, you can BYOI (Bring Your Own Infrastructure). + + +To follow this post, you’ll just need: + +- One or more nodes running the Docker daemon >= 1.12 +- At least a stand-alone Elasticsearch node 2.4.X + +Again, note that this post is not about setting up a production-ready Elasticsearch cluster. A single node cluster is not recommended for production. So if you’re planning a production installation, please refer to [Elastic guidelines][6]. + +### A friendly note for early adopters + +I’m usually an early adopter (and I’m already using the latest alpha version in production, of course). But for this post, I chose not to use the latest Elasticsearch 5.0.0 alpha. Their roadmap is not perfectly clear to me, and I don’t want be the root cause of your problems! + +So the Elasticsearch reference version for this post is the latest stable version, 2.4.0 at the moment of writing. + +### Test cluster setup script + +As said, I wanted to provide this script for everyone who would like to follow the blog without having to figure out how to create a Swarm cluster and install an Elasticsearch. Of course, you can skip this if you choose to use your own Swarm Mode engines and your own Elasticsearch nodes. + +To execute the setup script, you’ll need: + +- [Docker Machine][7] – latest version: to provision Docker engines on DigitalOcean +- [DigitalOcean API Token][8]: to allow docker-machine to start nodes on your behalf + +![](https://resources.codeship.com/hubfs/CTAs/EVAL/Codeship_Request_Trial_Access.png?t=1473869513342) + +### Create Cluster Script + +Now that you have everything we need, you can copy the following script in a file named create-cluster.sh: + +``` +#!/usr/bin/env bash +# +# Create a Swarm Mode cluster with a single master and a configurable number of workers + +workers=${WORKERS:-"worker1 worker2"} + +####################################### +# Creates a machine on Digital Ocean +# Globals: +# DO_ACCESS_TOKEN The token needed to access DigitalOcean's API +# Arguments: +# $1 the actual name to give to the machine +####################################### +create_machine() { + docker-machine create \ + -d digitalocean \ + --digitalocean-access-token=$DO_ACCESS_TOKEN \ + --digitalocean-size 2gb \ + $1 +} + +####################################### +# Executes a command on the specified machine +# Arguments: +# $1 The machine on which to run the command +# $2..$n The command to execute on that machine +####################################### +machine_do() { + docker-machine ssh $@ +} + +main() { + + if [ -z "$DO_ACCESS_TOKEN" ]; then + echo "Please export a DigitalOcean Access token: https://cloud.digitalocean.com/settings/api/tokens/new" + echo "export DO_ACCESS_TOKEN=" + exit 1 + fi + + if [ -z "$WORKERS" ]; then + echo "You haven't provided your workers by setting the \$WORKERS environment variable, using the default ones: $workers" + fi + + # Create the first and only master + echo "Creating the master" + + create_machine master1 + + master_ip=$(docker-machine ip master1) + + # Initialize the swarm mode on it + echo "Initializing the swarm mode" + machine_do master1 docker swarm init --advertise-addr $master_ip + + # Obtain the token to allow workers to join + worker_tkn=$(machine_do master1 docker swarm join-token -q worker) + echo "Worker token: ${worker_tkn}" + + # Create and join the workers + for worker in $workers; do + echo "Creating worker ${worker}" + create_machine $worker + machine_do $worker docker swarm join --token $worker_tkn $master_ip:2377 + done +} + +main $@ +``` + +And make it executable: + +``` +chmod +x create-cluster.sh +``` + +### Create the cluster + +As the name suggests, we’ll use the script to create the cluster. By default, the script will create a cluster with a single master and two workers. If you want to configure the number of workers, you can do that by setting the WORKERS environment variable. + +Now, let’s create that cluster! + +``` +./create-cluster.sh +``` + +Ok, now you can go out for a coffee. This will take a while. + +Finally the cluster is ready! + +-------------------------------------------------------------------------------- + +via: https://blog.codeship.com/monitoring-docker-containers-with-elasticsearch-and-cadvisor/ + +作者:[Lorenzo Fontana][a] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://blog.codeship.com/author/lorenzofontana/ + + + + + + + +[1]: https://github.com/elastic/elasticsearch +[2]: https://github.com/elastic/kibana +[3]: https://github.com/google/cadvisor +[4]: https://github.com/elastic/beats +[5]: https://twitter.com/share?text=%22How+do+you+keep+track+of+all+that%27s+happening+in+a+Swarm+Mode+cluster%3F+Not+easily.%22+via+%40fntlnz&url=https://blog.codeship.com/monitoring-docker-containers-with-elasticsearch-and-cadvisor/ +[6]: https://www.elastic.co/guide/en/elasticsearch/guide/2.x/deploy.html +[7]: https://docs.docker.com/machine/install-machine/ +[8]: https://cloud.digitalocean.com/settings/api/tokens/new From de56c577e577482b8c272152e422b6584e62e99f Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Sat, 17 Sep 2016 23:00:45 +0800 Subject: [PATCH 41/48] =?UTF-8?q?ucasFL=E7=BF=BB=E8=AF=91=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...tall Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md b/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md index 070d464077..516873c9ea 100644 --- a/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md +++ b/sources/tech/20160917 How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24.md @@ -1,3 +1,5 @@ +translating by ucasFL + How to Install Latest XFCE Desktop in Ubuntu 16.04 and Fedora 22-24 ==== From 6c9a5b1942185fe4327943872272cd8824bd62c5 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Sat, 17 Sep 2016 23:06:06 +0800 Subject: [PATCH 42/48] =?UTF-8?q?ucasFL=E7=BF=BB=E8=AF=91=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sources/tech/20160827 4 Best Linux Boot Loaders.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/tech/20160827 4 Best Linux Boot Loaders.md b/sources/tech/20160827 4 Best Linux Boot Loaders.md index 91deb14d58..7ea63cdb94 100644 --- a/sources/tech/20160827 4 Best Linux Boot Loaders.md +++ b/sources/tech/20160827 4 Best Linux Boot Loaders.md @@ -1,3 +1,5 @@ +translating by ucasFL + 4 Best Linux Boot Loaders ==== From 09c7446a1f3e8a7e94e35011cb9e7c2d34b5b1c8 Mon Sep 17 00:00:00 2001 From: Ezio Date: Sat, 17 Sep 2016 23:09:23 +0800 Subject: [PATCH 43/48] =?UTF-8?q?20160917-28=20=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ecurity Policy, Your Future Best Friend.md | 257 ++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 sources/tech/20160912 Content Security Policy, Your Future Best Friend.md diff --git a/sources/tech/20160912 Content Security Policy, Your Future Best Friend.md b/sources/tech/20160912 Content Security Policy, Your Future Best Friend.md new file mode 100644 index 0000000000..80f435bd4e --- /dev/null +++ b/sources/tech/20160912 Content Security Policy, Your Future Best Friend.md @@ -0,0 +1,257 @@ +Content Security Policy, Your Future Best Friend +===== + +A long time ago, my personal website was attacked. I do not know how it happened, but it happened. Fortunately, the damage from the attack was quite minor: A piece of JavaScript was inserted at the bottom of some pages. I updated the FTP and other credentials, cleaned up some files, and that was that. + +One point made me mad: At the time, there was no simple solution that could have informed me there was a problem and — more importantly — that could have protected the website’s visitors from this annoying piece of code. + +A solution exists now, and it is a technology that succeeds in both roles. Its name is content security policy (CSP). + +### What Is A CSP? Link + +The idea is quite simple: By sending a CSP header from a website, you are telling the browser what it is authorized to execute and what it is authorized to block. + +Here is an example with PHP: + +``` +"); +?> +``` + +#### SOME DIRECTIVES LINK + +You may define global rules or define rules related to a type of asset: + +``` +default-src 'self' ; + # self = same port, same domain name, same protocol => OK +``` + +The base argument is default-src: If no directive is defined for a type of asset, then the browser will use this value. + +``` +script-src 'self' www.google-analytics.com ; + # JS files on these domains => OK +``` + +In this example, we’ve authorized the domain name www.google-analytics.com as a source of JavaScript files to use on our website. We’ve added the keyword 'self'; if we redefined the directive script-src with another rule, it would override default-src rules. + +If no scheme or port is specified, then it enforces the same scheme or port from the current page. This prevents mixed content. If the page is https://example.com, then you wouldn’t be able to load http://www.google-analytics.com/file.js because it would be blocked (the scheme wouldn’t match). However, there is an exception to allow a scheme upgrade. If http://example.com tries to load https://www.google-analytics.com/file.js, then the scheme or port would be allowed to change to facilitate the scheme upgrade. + +``` +style-src 'self' data: ; + # Data-Uri in a CSS => OK +``` + +In this example, the keyword data: authorizes embedded content in CSS files. + +Under the CSP level 1 specification, you may also define rules for the following: + +- `img-src` + +valid sources of images + +- `connect-src` + +applies to XMLHttpRequest (AJAX), WebSocket or EventSource + +- `font-src` + +valid sources of fonts + +- `object-src` + +valid sources of plugins (for example, `, , `) + +- `media-src` + +valid sources of `